Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello How to calculate the diffe

Status
Not open for further replies.

jmarioscs

MIS
Oct 16, 2012
3
BR
Hello

How to calculate the difference in days between the dates below ??

20120915 e 20121017

 
A way ....

Code:
#!/bin/ksh

# This function takes a date time string of the format:
# YYYY MM DD HH MM SS
# perl autosplits the string and uses timelocal to return
# the number of seconds from the Epoch.
# No error checking!
function seconds_from_epoch {
  echo $*| perl -MTime::Local -ane '
  my $epochseconds = timelocal($F[5], $F[4], $F[3], $F[2], $F[1] - 1, $F[0]);
 print "$epochseconds\n"; '
}

dt1=20120915
y1=$(echo "$dt1"|cut -c1-4)
m1=$(echo "$dt1"|cut -c5-6)
d1=$(echo "$dt1"|cut -c7-8)
s1=$(seconds_from_epoch "$y1 $m1 $d1 0 0 0")
echo $s1

dt2=20121017
y2=$(echo "$dt2"|cut -c1-4)
m2=$(echo "$dt2"|cut -c5-6)
d2=$(echo "$dt2"|cut -c7-8)
s2=$(seconds_from_epoch "$y2 $m2 $d2 0 0 0")
echo $s2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top