【发布时间】:2021-12-13 09:56:53
【问题描述】:
我的例子是:How to compare dates using perl?
...
# $ARG1 is specified by the user for example "10" min
my $difftime=$ARG1;
# -- Get date ---------------------------------------------------------------------
#Stores current date and time - $time min
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time() - (60*$difftime));
$year += 1900;
#parse date format to '22.10.2021 00:00:00' -----------------------------------------------------
my $act_date="$mday.$mon.$year $hour:$min:$sec";
...
我的第一个问题是 $hour 和 $mon 是从一个到低,为什么?
我的第二个问题是时间是一位数“8:4:34”而不是两位数“08:04:34”。这是日期比较的问题吗?
...
#compare date
# date example for $sql_value is 06.10.2021 09:38:27
my $date_to_compare=$sql_value;
if ($act_date <= $date_to_compare)
{
print "the current date is smaller";
}
else
{
print "the current date is greater";
}
...
我只得到“当前日期较小”为什么?
另外,我怎样才能让时间以分钟为单位?
谢谢
【问题讨论】:
-
请,每个帖子只问一个问题。您提到您以问题How to compare dates using Perl 为例。接受的答案建议使用
Time::Piece来执行此操作,但您不使用此模块;这是为什么?它会让您的生活更轻松,特别是因为Time::Piece的文档解释了如何解决您的大部分问题。 -
或来自 CPAN 的
DateTime。 -
@Apfelkuchen - 日期字符串只有在订购 year-mon-mday 时才能进行合理比较。
-
一个非常相关的post,以及小时/分钟的差异。两个模块也一样