• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1214
  • Last Modified:

Date difference

Hi,

Is there a way to find out the month difference between two dates in UNIX shell script?

Thanks
Roy
0
dib_roy
Asked:
dib_roy
2 Solutions
 
mbekkerCommented:
Hi dib_roy,

To determine only the number of months you could try the following script:

#!/usr/bin/ksh

monthstr_to_num()
{
 case "$1" in
   Jan) echo "1" ;;
   Feb) echo "2" ;;
   Mar) echo "3" ;;
   Apr) echo "4" ;;
   May) echo "5" ;;
   Jun) echo "6" ;;
   Jul) echo "7" ;;
   Aug) echo "8" ;;
   Sep) echo "9" ;;
   Oct) echo "10" ;;
   Nov) echo "11" ;;
   Dec) echo "12" ;;
 esac
}

echo "$1" | read month1 year1
echo "$2" | read month2 year2

year_diff=$(( ( $year2 - $year1 ) * 12 ))  # in months...
month_diff=$(( $(monthstr_to_num $month2) - $(monthstr_to_num $month1) ))
total_diff=$(( $month_diff + $year_diff ))
echo "Difference between the months $month1 $year1 and $month2 $year2 is: $total_diff"


Usage: month_diff <"month1 year1"> <"month2 year2">

It depents on how the datestamps look like. Perhaps the months are already in numbers so you could leave out the convert part.

If you've got normal datestamps like:

   Mon Dec  1 12:40:32 CET 2003

you can awk,sed,cut,... the parts out you want. In this example it would be:
awk {'print $2 " " $6'}

Good luck!
0
 
HamdyHassanCommented:

You could check Perl
use Time::Local;

http://www.perldoc.com/perl5.6/lib/Time/Local.html


for example to get yesterday date using perl

use Time::Local;
$today = timelocal(localtime);
# Subtract 1 day
@yesterday = localtime($today - (24 * 60 * 60));
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
$today = timelocal(localtime);
is a roundabout way of saying
$today = time;


What is the format of the dates you wish to find the difference of?
0
 
cluedonCommented:

hard with a shell script, easy with a perl script.    If perl is an option you shoud repost your question in a perl section.
you should also post the format of the dates you want to compare, there are a lot of variations.

0
 
ozoCommented:
How many months difference is there between 2003-12-31 and 2004-01-01?
0
 
dib_royAuthor Commented:
Thanks everybody for your help. I got my answer.

Roy
0
 
liddlerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Points split mbekker & HamdyHassan

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now