How to check dates in Perl?

How can I check date strings in Perl? I would like to consider leap years as well as time zones. Someone may enter dates in the following formats:

  11/17/2008
 11/17/2008 3pm
 11/17/2008 12:01 am
 11/17/2008 12:01 am EST
 11/17/2008 12:01 am CST
+6
datetime perl
source share
3 answers

I am using DateTime :: Format :: DateManip for such things. Using your dates ....

use DateTime::Format::DateManip; my @dates = ( '11/17/2008', '11/17/2008 3pm', '11/17/2008 12:01am', '11/17/2008 12:01am EST', '11/17/2008 12:01am CST', ); for my $date ( @dates ) { my $dt = DateTime::Format::DateManip->parse_datetime( $date ); die "Cannot parse date $date" unless defined $dt; say $dt; } # no dies.. produced the following.... # => 2008-11-17T00:00:00 # => 2008-11-17T15:00:00 # => 2008-11-17T00:01:00 # => 2008-11-17T05:01:00 # => 2008-11-17T06:01:00 

NB. I am here in GMT!

+10
source share

CPAN has many packages for processing dates, such as DateTime and Date :: Manip . To analyze your date, the Date :: Parse module in TimeDate can work (yes, there are both DateTime and TimeDate).

In general, whenever you need to do something in Perl, check out CPAN. There is probably a module for this. See what is available and see what is appropriate for your situation.

Good luck :)

+9
source share

I will post my answers here when I stumbled upon them.

use time :: local
http://www.perlmonks.org/?node_id=642627

0
source share

All Articles