Visit The New, Modern Unix Linux Community


Months difference between 2 dates


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Months difference between 2 dates
# 1  
Months difference between 2 dates

Hello,
I would like to find out the number of months between two dates as below example.

Code:
 date 1 = 03-02-2016
 date 2 = 15-11-2015

I need 04 as months difference.

Any help on this is highly appreciated.

Thanks,
Keerti

Last edited by Don Cragun; 02-03-2016 at 06:57 AM.. Reason: Add CODE and ICODE tags.
# 2  
Hello keertis,

Welcome to forums, please use code tags as per forum rules for commands/codes/Inputs which you use into your posts.
Following may help you in same. Here I am considering that variable named date1's year value will be either equal or greater than the year's value of variable named date2. Here are the 2 examples which I have tried.
1st: Your provided Input_file as follows.
Code:
awk -vdate1="03-02-2016" -vdate2="15-11-2015" 'BEGIN{split(date1, A,"-");split(date2, B,"-");year_diff=A[3]-B[3];if(year_diff){months_diff=A[2] + 12 * year_diff - B[2] + 1;} else {months_diff=A[2]>B[2]?A[2]-B[2]+1:B[2]-A[2]+1};print months_diff}'

Output will be 4, as per your requirement shown.
2nd: In case(Here I am taking an example) where year's values in both variables are equal then following may help in same.
Code:
awk -vdate1="03-02-2015" -vdate2="15-11-2015" 'BEGIN{split(date1, A,"-");split(date2, B,"-");year_diff=A[3]-B[3];if(year_diff){months_diff=A[2] + 12 * year_diff - B[2] + 1;} else {months_diff=A[2]>B[2]?A[2]-B[2]+1:B[2]-A[2]+1};print months_diff}'

Output will be 10.
If this above doesn't meet your requirement then please do let us know all the scenarios of your request with your O.S details too, hope this helps.


EDIT: Also wanted to add 3rd point here if year difference is more than 1 then also it should work following is the example for same.
Code:
awk -vdate1="03-02-2017" -vdate2="15-11-2015" 'BEGIN{split(date1, A,"-");split(date2, B,"-");year_diff=A[3]-B[3];if(year_diff){months_diff=A[2] + 12 * year_diff - B[2] + 1;} else {months_diff=A[2]>B[2]?A[2]-B[2]+1:B[2]-A[2]+1};print months_diff}'

Output will be 16 then.

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-03-2016 at 12:54 AM.. Reason: Added one more condition where years difference is more than 1 year.
# 3  
What is your OS and date command version?

---------- Post updated at 10:27 ---------- Previous update was at 10:17 ----------

And, assuming your date format is dd-mm-yyyy, and the first date is 3. Feb, HOW do you get 4 months? Actually, it is 2 months and 18 days, rounding up to 3 months.
# 4  
Hi.

Using an available module in a package that deals with dates:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate a month duration between 2 dates, ddiff.

program=ddiff
program=dateutils.ddiff

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C $program

date1=03-02-2016
date2=15-11-2015

pl " Result difference in months:"
$program -i"%d-%m-%Y" "$date2" "$date1" -f "%m %d"

pl " A method for rounding up:"
read months days  < <( $program -i"%d-%m-%Y" "$date2" "$date1" -f "%m %d" )
if [ "$days" -gt 0 ]
then
  (( months++ ))
fi
pe " Rounded-up months is $months"

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.3 (jessie) 
bash GNU bash 4.3.30
dateutils.ddiff ddiff 0.3.1

-----
 Result difference in months:
2 19

-----
 A method for rounding up:
 Rounded-up months is 3

See repository and man pages for details on ddiff in dateutils. Found in a number of distributions including Debian-related, OS X (brew), etc.

Best wishes ... cheers, drl
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #877
Difficulty: Medium
A real-time operating system (RTOS) is an operating system (OS) intended to serve real-time applications that process data as it comes in, typically without buffer delays.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Difference between two dates

Hi There I am trying to find the difference between two dates in seconds, by taking the first 10 digits of the file name itself, which I have done as shown below: current_time=`date +%s` last_login_of_tim=`date -d @1489662376 +%s` diff_sec=$(($current_time-$last_login_of_tim)) ... (5 Replies)
Discussion started by: simpsa27
5 Replies

2. Fedora

Difference of dates

I have a script which is printing date in below format while writing the logs. theDate=`date +"%m%d%Y"` theTime=`date +"%H%M%S"` echo $theDate $theTime How can i find out difference current time and above format. Appreciate your help. (6 Replies)
Discussion started by: srikanth38
6 Replies

3. Shell Programming and Scripting

Difference between 2 dates

Hi Friends, I have a file that has the contents like below: file1.txt 5,13/07/2013 23:25:25,14/07/2013 19:40:21 5,13/07/2013 23:25:25,14/07/2013 19:40:43 5,12/07/2013 23:50:50,13/07/2013 20:30:26 5,12/07/2013 23:20:24,13/07/2013 19:40:53 60,14/07/2013 00:00:00,14/07/2013 23:00:39... (5 Replies)
Discussion started by: vsachan
5 Replies

4. Shell Programming and Scripting

Difference between two dates

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 (2 Replies)
Discussion started by: gani_85
2 Replies

5. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

6. Shell Programming and Scripting

Difference between two dates

Hi! I have two parameters like this: YYYY-MM-DD YYYY-MM-DD My question is, there is a direct command for get the elapsed time between the 2 dates, or I have to find another way? Thx! (1 Reply)
Discussion started by: MalaTomi
1 Replies

7. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Difference between two dates.

Hi all. My question may seems to be similar to one that already been here. But i need a little other solution. I have two dates in format dd/mm/yyyy. I need to find number of days between them. I need to do it in bash script. I am running on Solaris machine and have cutted 'date' command version... (1 Reply)
Discussion started by: kukuruku
1 Replies

9. Shell Programming and Scripting

Difference between two dates...

Hi All, Wish you a Happy New year... I have to find the difference between two dates, the result should be the number of days. I have seen the "datecalc" function. Its good, can I have any other alternative. Thanks in Advance Raju (4 Replies)
Discussion started by: rajus19
4 Replies

10. UNIX for Dummies Questions & Answers

deleting files with dates 3 months ago

please help me with this????? :confused: :confused: i need to create a program that will run in unix that will delete all files in a given directory that is at least 3 months old. first the program will need to automatically know what date it is right now to determine the files it will... (3 Replies)
Discussion started by: godalle
3 Replies

Featured Tech Videos