在将它们转换为纪元后,我计算了两个ISO 8601日期的差异。 如何在天数中得出它们的差异? 我的代码是
my $ResolvedDate = "2014-06-04T10:48:07.124Z";
my $currentDate = "2014-06-04T06:03:36-04:00"
my $resolved_epoch = &convert_time_epoch($ResolvedDate);
my $current_epoch = &convert_time_epoch($currentDate);
if (($resolvedDate - $currentDate) > $noOfDays) {
print "Difference in greater than x\n";
$built = 0;
return ($built);
} else {
print "Difference in smaller than x \n";
$built = 1;
return ($built);
}
sub convert_time_epoch {
my $time_c = str2time(@_);
my @time_l = localtime($time_c);
my $epoch = strftime("%s", @time_l);
return($epoch);
}
除了$ built之外,我还要返回确切的天数,已解决的日期大于当前日期。
答案 0 :(得分:5)
“天数”很尴尬,因为这是本地时间和DST存在(或者至少可能存在)。
通过简单地除以86400,您可以轻松获得24小时的数量,这可能足以达到您的目的。
如果您想要mday
字段发生变化的真实次数,则可能与此简单除法所获得的值略有不同。
答案 1 :(得分:1)
如果日期是在纪元秒中,请取差值并将其除以一天中的秒数(即86400)。像这样:
my $days_difference = int(($time1 - $time2) / 86400);
如果您使用DateTime,那么
my $duration = $dt1->delta_days($dt2); #$dt1 and $dt2 are DateTime objects.
print $duration->days;
答案 2 :(得分:1)
use DateTime::Format::ISO8601 qw( );
my $ResolvedDate = "2014-06-04T10:48:07.124Z";
my $currentDate = "2014-06-04T06:03:36-04:00";
my $format = DateTime::Format::ISO8601->new();
my $dt_resolved = $format->parse_datetime($ResolvedDate);
my $dt_current = $format->parse_datetime($currentDate);
my $dur = $dt_resolved->delta_days($dt_current);
my $days = $dur->in_units('days');