在 Perl 中查找天数

Find number of days in Perl

我有两个日期格式为年-月-日hour:minute:sec

例如:2015-01-01 10:00:002015-01-10 11:00:00。我想将这两天之间的天数计算为 10

我尝试了本地时间功能,但没有用。我需要在 perl 中解决这个问题。请帮忙。

试试这个:

(
    Time::Piece->strptime('2015-01-01 10:00:00', '%Y-%m-%d %H:%M:%S')
  - Time::Piece->strptime('2015-01-10 11:00:00', '%Y-%m-%d %H:%M:%S')
)->days

因为 v5.9.5 Time::Piece 是核心 Perl 发行版的一部分

Time::Piece 一段时间以来一直是 Perl 中的标准模块:

use strict;
use warnings;

use feature qw(say);
use Time::Piece;

my $date1 = '2015-01-01 10:00:00';
my $date2 = '2015-01-10 11:00:00';

my $format = '%Y-%m-%d %H:%M:%S';

my $diff = Time::Piece->strptime($date2, $format)
   - Time::Piece->strptime($date1, $format);

# subtraction of two Time::Piece objects produces a Time::Seconds object 
say $diff->days;