Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

Converting date to specified format and two date comparisions

Given Two Dates:

$Date1

format: yyyy-mm-dd hh:mm:ss eg:  2013-05-21 07:47:21

$Date2

format: Day Month date hh:mm:ss yyyy eg: Thu Aug  1 09:53:40 2013

How to compare $Date1 and $Date2?

I want to do following operation:

if($Date2>=$Date1){ //Do some operation }

But I am not able to make comparison as the two dates are in different Format.

Upvotes: 0

Views: 142

Answers (2)

Toto
Toto

Reputation: 91385

How about:

use Time::Piece;
my $d1 = '2013-05-21 07:47:21';
my $d2 = 'Thu Aug  1 09:53:40 2013';
my $t1 = Time::Piece->strptime($d1, "%Y-%m-%d %T");
my $t2 = Time::Piece->strptime($d2, "%c");
say $t2->epoch - $t1->epoch;

output:

6228379

Time::Piece documentation

Upvotes: 4

PP.
PP.

Reputation: 10864

Parse the date using regular expressions and use the Time::Local functions to convert into seconds past 1 Jan 1970 to compare integers.

Upvotes: 0

Related Questions