How to compare string date time in perl?

后端 未结 6 828
醉梦人生
醉梦人生 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:20

    One more solution that uses the overloaded comparisons after converting the times to Time::Piece objects. Creating the objects may be overkill for something simple, but they can become very useful if you need to do other things with the times.

    use Time::Piece;
    
    my $dateformat = "%H:%M:%S, %m/%d/%Y";
    
    my $date1 = "11:56:41, 11/22/2011";
    my $date2 = "11:20:41, 11/20/2011";
    
    $date1 = Time::Piece->strptime($date1, $dateformat);
    $date2 = Time::Piece->strptime($date2, $dateformat);
    
    if ($date2 < $date1) {
        do something...
    } else {
        do nothing...
    }
    

提交回复
热议问题