Solved

Date difference

Posted on 2003-12-01
9
1,166 Views
Last Modified: 2013-12-26
Hi,

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

Thanks
Roy
0
Comment
Question by:dib_roy
9 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 9849308
0
 
LVL 1

Accepted Solution

by:
mbekker earned 125 total points
ID: 9849517
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
 
LVL 9

Assisted Solution

by:HamdyHassan
HamdyHassan earned 125 total points
ID: 9850539

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
 
LVL 84

Expert Comment

by:ozo
ID: 9854428
$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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:cluedon
ID: 10027173

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
 
LVL 84

Expert Comment

by:ozo
ID: 10027457
How many months difference is there between 2003-12-31 and 2004-01-01?
0
 

Author Comment

by:dib_roy
ID: 10027675
Thanks everybody for your help. I got my answer.

Roy
0
 
LVL 18

Expert Comment

by:liddler
ID: 10241346
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 99
sum28 challenge 31 107
withoutTen challenge 14 101
sumHeights  challenge 17 66
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now