hi all,
I need a help for below requirement.
Difference between two dates"12-11-2009" and "03-25-2012" (mm-dd-yy format") in weeks and days and hours
Please help me for this. Thanks in adv....
I am working in AIX, so dont have below command:-
date --version
[highlight=perl]#! /usr/bin/perl -w
use strict;
use Time::Local 'timelocal';
my ($frm_dt, $to_dt) = ($ARGV[0], $ARGV[1]);
my $diff = dt_to_epoch ($to_dt) - dt_to_epoch ($frm_dt);
my $x = $diff / (60*60*24*7);
print $diff / (60*60*247), " week(s) OR\n";
print $diff / (60*6024) , " day(s) OR\n" ;
print $diff / (60*60) , " hr(s)\n\nOR\n" ;
if ($x > int ($x)) {
print int($x), " week(s) ";
$x = ($x - int($x)) * 7;
print int($x), " day(s) ";
$x = ($x - int($x)) * 24;
print int($x), " hr(s)\n";
}
sub dt_to_epoch {
my $date = shift;
my @x = split /-/, $date;
return timelocal (0, 0, 0, $x[1], $x[0] - 1, $x[2]);
}[/highlight]
[root@host user]# ./test.pl "12-11-2009" "03-25-2012"
119.285714285714 week(s) OR
835 day(s) OR
20040 hr(s)
OR
119 week(s) 2 day(s) 00 hr(s)
Another perl:
# cat gap.pl
#!/usr/bin/env perl
use strict;
use Time::Local;
sub diffT {
my ($initD,$endD)=@_;
my @initD = split /-/, $initD;
my @endD = split /-/, $endD;
int(timelocal(0,0,0,$initD[1],$initD[0]-1,$initD[2]) - timelocal(0,0,0,$endD[1],$endD[0]-1,$endD[2])) ;
}
my $init=shift;
my $end=shift;
my $diff=diffT($init,$end);
my $Ws = int( $diff / 604800 ) ;
my $Ds = ( $diff - ( 604800 * $Ws ) ) / 86400;
my $Hs = ( $diff - ( ( 604800 * $Ws ) + ($Ds * 86400) ) ) / 3600 ;
print "Weeks: $Ws Days:$Ds Hours:$Hs\n";
exit 0
# gap.pl "03-25-2012" "12-11-2009"
Weeks: 119 Days:2 Hours:0