How to compare string date time in perl?

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

    An efficient method is to reorder the fields to something lexically comparable.

    sub to_comparable {
       my ($date) = @_;
       my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2}), ([0-9]{2})/([0-9]{2})/([0-9]{4})\z}
          or die;
       return "$Y$m$d$H$M$S";
    }
    
    if (to_comparable($date2) lt to_comparable($date1)) {
       ...
    } else {
       ...
    }
    

提交回复
热议问题