计算两个日期之间的天数/周/月/年

时间:2014-08-24 23:17:32

标签: perl datetime

如何计算在两个日期之间开始的当天(整数)天/周/月/年?

日期以time函数提供的纪元秒提供。这些示例显示根据ISO 8601格式化的时间。

1 day begin  between 2014-08-24T23:59:59+0200 and 2014-08-25T00:00:01+0200   
0 day begins between 2014-08-25T00:00:01+0200 and 2014-08-25T23:59:59+0200   

1 个答案:

答案 0 :(得分:2)

使用DateTime查找时间戳DT1和DT2之间的日/周/月/年开始次数,

  1. 查找DT1所属日/周/月/年的第一秒的时间戳。
  2. 查找DT2所属日/周/月/年的第一秒的时间戳。
  3. 查找这两个时间戳之间的天/周/月/年的差异。

  4. use DateTime qw( );
    
    # Or maybe << time_zone => 'local' >>?
    my $dt1 = DateTime->from_epoch( epoch => ..., time_zone => '+0200' );
    my $dt2 = DateTime->from_epoch( epoch => ..., time_zone => '+0200' );
    
    # Some days in some time zones don't have a midnight, so switch to 'floating'.
    # before truncating. The dts must all be in the same time zone beforehand.
    my $date1 = $dt1->clone;
    my $date2 = $dt2->clone;
    $_->set_time_zone('floating')->truncate( to => 'day' )
       for $date1, $date2;
    
    my $day_starts = $date2->delta_days($date1)->in_units('days');
    
    my $week_starts = do {                   # Sunday starts
       my $date1_week_start = $date1->clone;
       my $date2_week_start = $date2->clone;
    
       $_->subtract( days => $_->day_of_week % 7 )
          for $date1_week_start, $date2_week_start;
    
       $date2_week_start->delta_days($date1_week_start)->in_units('weeks')
    };
    
    my $month_starts = do {
       my $date1_month_start = $date1->clone;
       my $date2_month_start = $date2->clone;
    
       $_->truncate( to => 'month' )
          for $date1_month_start, $date2_month_start;
    
       $date2_month_start->delta_md($date1_month_start)->in_units('months')
    };
    
    my $year_starts = abs( $date2->year - $date1->year );
    
    printf("%d years, %d months, %d weeks and %d days begin between %s and %s\n",
       $year_starts,
       $month_starts,
       $week_starts,
       $day_starts,
       $date1->ymd,
       $date2->ymd,
    );
    

    my $dt1 = DateTime->new( year => 2014, month => 8, day => 23 );
    my $dt2 = DateTime->new( year => 2014, month => 8, day => 24 );
    

    给出

    0 years, 0 months, 1 weeks and 1 days begin between 2014-08-23 and 2014-08-24
    

    my $dt1 = DateTime->new( year => 2014, month => 1, day => 31 );
    my $dt2 = DateTime->new( year => 2014, month => 2, day =>  1 );
    

    给出

    0 years, 1 months, 0 weeks and 1 days begin between 2014-01-31 and 2014-02-01
    

    注意:

    • 我认为有问题的日期之间没有任何日子丢失。
    • 如果早期时间戳位于$dt1$dt2,则无关紧要。