本文介绍了如何验证Perl中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Perl中验证日期字符串?我想考虑闰年和时区。有人可能会按以下格式输入日期:
How can I validate date strings in Perl? I'd like to account for leap years and also time zones. Someone may enter dates in the following formats:
11/17/2008
11/17/2008 3pm
11/17/2008 12:01am
11/17/2008 12:01am EST
11/17/2008 12:01am CST
推荐答案
我使用这样的东西。使用你的日期....
I use DateTime::Format::DateManip for things like this. Using your dates....
use DateTime::Format::DateManip;
my @dates = (
'11/17/2008',
'11/17/2008 3pm',
'11/17/2008 12:01am',
'11/17/2008 12:01am EST',
'11/17/2008 12:01am CST',
);
for my $date ( @dates ) {
my $dt = DateTime::Format::DateManip->parse_datetime( $date );
die "Cannot parse date $date" unless defined $dt;
say $dt;
}
# no dies.. produced the following....
# => 2008-11-17T00:00:00
# => 2008-11-17T15:00:00
# => 2008-11-17T00:01:00
# => 2008-11-17T05:01:00
# => 2008-11-17T06:01:00
我在GMT这里!
/ I3az /
这篇关于如何验证Perl中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!