It returns "0 days of difference".use Modern::Perl; use DateTime; use DateTime::Format::Flexible; my $first_dt = DateTime::Format::Flexible->parse_datetime( 'Fri, 26 Au +g 2011 14:34:55 GMT'); my $second_dt = DateTime::Format::Flexible->parse_datetime( 'Sat, 27 A +ug 2011 07:03:02 +1000'); my $first_dt_utc = $first_dt->set_time_zone('UTC'); my $second_dt_utc = $second_dt->set_time_zone('UTC'); say $first_dt_utc->delta_days( $second_dt_utc )->delta_days, ' days of + difference';
The delta_days method takes into account only the date part of the DateTime object and returns a DateTime::Duration object and the delta_days method (of the DateTime::Duration-object, nothing to do with the delta_days method of the DateTime-object! Speaking of confusion here) converts that into a number of days.
Or without any intermediary steps or variables:
Update: Changed the ->days method to the ->delta_days method as per Jim's excellent suggestion.use Modern::Perl; use DateTime; use DateTime::Format::Flexible; say DateTime::Format::Flexible->parse_datetime( 'Fri, 26 Aug 2011 14:3 +4:55 GMT')->set_time_zone('UTC')->delta_days(DateTime::Format::Flexib +le->parse_datetime( 'Sat, 27 Aug 2011 07:03:02 +1000')->set_time_zone +('UTC'))->delta_days, ' days of difference';
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
In reply to Re: How to compare date/times in different timezones?
by CountZero
in thread How to compare date/times in different timezones?
by philled
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |