How to compare string date time in perl?

后端 未结 6 831
醉梦人生
醉梦人生 2021-01-13 10:37

I have this date time format in string \"11:56:41, 11/22/2011\".

Here\'s what I want:

Compare two date time strings like.

$date1 = \"11:56:41         


        
6条回答
  •  遥遥无期
    2021-01-13 11:11

    I use unixtime. :)

    I convert both times to unixtime and then I just have two integers to compare and so I can use the operators <, ==, > etc

    e.g. convert to unixtime as follows

    my $timestamp = "2014-03-25 12:33:32";  # (We assume localtime)
    
    # 
    # To split on the space character, it's best to use the regex / /
    # 
    my ($date, $time) = split (/ /, $timestamp);
    my ($year, $mon, $mday) = split ('-', $date);
    my ($hour, $min, $sec) = split (':', $time);
    
    my $unixtime = timelocal($sec, $min, $hour, $mday, $mon-1, $year);
    

提交回复
热议问题