【问题标题】:Matching a date in perl在 perl 中匹配日期
【发布时间】:2010-04-04 03:32:22
【问题描述】:

我想匹配格式为日/月/年的日期。其中日为两位数,月为两位数,年为四位。另外,我想看看它是否是一个有效的日期,例如知道什么时候是闰年,知道哪个月份有 30 天、31 天和 28 天,或者 2 月有 29 天。

【问题讨论】:

标签: regex perl


【解决方案1】:

看看像Date::Manip这样的东西;当这样的事情可用时,自己做这件事没有什么意义。

$date = ParseDate($mydate);
unless ($date) {
  # error
}
...

【讨论】:

  • 谢谢,它似乎适用于月/日/年格式,但我无法让它适用于日/月/年格式。
  • @Zerobu 10/12/2010 是模棱两可的。 Date::Manip 采用美式风格,10 月 12 日。你必须告诉它否则。有关详细信息,请参阅 search.cpan.org/~sbeck/Date-Manip-5.56/lib/Date/… 中的 DateFormat。
【解决方案2】:

使用下面的代码

use strict;
use warnings;
use Date::Manip;
my $start="2010:03:30:23:02:3";
my $split=":";
my($year,$month,$date,$hour,$min,$sec);

($year,$month,$date,$hour,$min,$sec)=split($split,$start);

my $result = ParseDate("$month/$date/$year");
if(!$result)
{
    print "Invalid Date\n";
    exit;
}

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多