How to calculate date difference in perl

I am new to perl scripts. I have a requirement when I need to find the difference of two dates in minutes / seconds

$date1 = Fri Aug 30 10:53:38 2013 $date2 = Fri Aug 30 02:12:25 2013 

Can you tell me how we achieve this, analysis, calculation, req modules and all

Thanks goutham

+7
date datetime perl
source share
2 answers

Time :: Piece has been a standard part of Perl since 2007.

 #!/usr/bin/perl use strict; use warnings; use 5.010; use Time::Piece; my $date1 = 'Fri Aug 30 10:53:38 2013'; my $date2 = 'Fri Aug 30 02:12:25 2013'; my $format = '%a %b %d %H:%M:%S %Y'; my $diff = Time::Piece->strptime($date1, $format) - Time::Piece->strptime($date2, $format); say $diff; 
+14
source share

Convert both dates to UNIX

See http://metacpan.org/pod/Date::Parse

Then you can do a simple mathematical subtraction to find the number of seconds between them.

Then these are simple mathematicians to get minutes, hours, etc.

+1
source share

All Articles