How to compare string date in perl?

I have a date format in the line "11:56:41, 11/22/2011".

Here is what I want:

Compare two lines of time, for example.

$date1 = "11:56:41, 11/22/2011";
$date2 = "11:20:41, 11/20/2011";
if($date2 < $date1) {
    do something...
} else {
    do nothing...
}

Any ideas how I can achieve this in perl?

+5
source share
6 answers

An effective method is to change the order of the fields to something lexically comparable.

sub to_comparable {
   my ($date) = @_;
   my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2}), ([0-9]{2})/([0-9]{2})/([0-9]{4})\z}
      or die;
   return "$Y$m$d$H$M$S";
}

if (to_comparable($date2) lt to_comparable($date1)) {
   ...
} else {
   ...
}
+11
source

Another solution using overloaded comparisons after converting time to objects Time::Piece. Creating objects may be redundant for something simple, but they can become very useful if you need to do other things over time.

use Time::Piece;

my $dateformat = "%H:%M:%S, %m/%d/%Y";

my $date1 = "11:56:41, 11/22/2011";
my $date2 = "11:20:41, 11/20/2011";

$date1 = Time::Piece->strptime($date1, $dateformat);
$date2 = Time::Piece->strptime($date2, $dateformat);

if ($date2 < $date1) {
    do something...
} else {
    do nothing...
}
+12
source

, 4 DateTime ( DateTime) ? , ... ☻

use DateTime::Format::Strptime qw();
my $p = DateTime::Format::Strptime->new(pattern => '%T, %D', on_error => 'croak',);

my $date1 = $p->parse_datetime('11:56:41, 11/22/2011');
my $date2 = $p->parse_datetime('11:20:41, 11/20/2011');

if($date2 < $date1) {
    say "$date2 comes before $date1";
} else {
    say "$date2 does not come before $date1";
}

parse_datetime DateTime, DTRT.

+5

unixtime.:)

unixtime, , <, ==, > etc ..

. unixtime

my $timestamp = "2014-03-25 12:33:32";  # (We assume localtime)

# 
# To split on the space character, it best to use the regex / /
# 
my ($date, $time) = split (/ /, $timestamp);
my ($year, $mon, $mday) = split ('-', $date);
my ($hour, $min, $sec) = split (':', $time);

my $unixtime = timelocal($sec, $min, $hour, $mday, $mon-1, $year);
+2

( , ) ISO8601, .

,

HH:MM:SS, mm/DD/YYYY

ISO 8601:

YYYY-MM-DDTHH:MM:SS

.

. http://codepad.org/berle9um

Repeats here:

sub my_format_to_iso8601 {
    $_[0] =~ /(\d\d):(\d\d):(\d\d), (\d\d)\/(\d\d)\/(\d\d\d\d)/;
    return "$6-$4-$5T$1:$2:$3";
}

$date1 = "11:56:41, 11/22/2011";
$date2 = "11:20:41, 11/20/2011";
$d1 = my_format_to_iso8601($date1);
$d2 = my_format_to_iso8601($date2);
print "first is $d1\n";
print "second is $d2\n";
if ($d2 < $d1) {
    print "second is earlier\n";
} else {
    print "first is earlier\n";
}

ADDITION

  • Ikegami Perl code is much better.
  • The date library will be your friend; just specify the format string and use the library parsing function to get your date object, which then should be able to compare directly. (However, it is always interesting to note that ISO8601 by design is sorted in string form.)
+1
source

In this case, I always used Date::Calc:

use Date::Calc;

my $date1 = "11:56:41, 11/22/2011";
my $date2 = "11:20:41, 11/20/2011";

my @date1arr=split /[^\d]/, $date1 if($date1 =~ m!\d{2}:\d{2}:\d{2}, \d{2}/\d{2}/\d{4}!;
my @date2arr=split /[^\d]/, $date2 if($date2 =~ m!\d{2}:\d{2}:\d{2}, \d{2}/\d{2}/\d{4}!;

my @diff = Delta_DHMS(@date1arr, @date2arr);
my $less;
foreach my $d ( @diff ) { $less = 1 if $d < 0; }

if($less) { ... }
+1
source

All Articles