如何在perl中比较字符串日期时间? [英] How to compare string date time in perl?
本文介绍了如何在perl中比较字符串日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在字符串11:56:41, 11/22/2011"中有这个日期时间格式.
I have this date time format in string "11:56:41, 11/22/2011".
这是我想要的:
比较两个日期时间字符串,如.
Compare two date time strings like.
$date1 = "11:56:41, 11/22/2011";
$date2 = "11:20:41, 11/20/2011";
if($date2 < $date1) {
do something...
} else {
do nothing...
}
有什么想法可以在 perl 中实现吗?
Any ideas how could i achieve this in perl?
推荐答案
一种有效的方法是将字段重新排序为词汇上可比较的内容.
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 {
...
}
这篇关于如何在perl中比较字符串日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文