Get number of days between 2 dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get number of days between 2 dates
# 1  
Old 05-12-2017
Get number of days between 2 dates

Gents.
Please can u help.
I would like to calculate the days between two dates.
Example file1 ( previous date)
file1 - Input file
Code:
9/29/2010 10195
9/29/2010 1057
2/2/2016 10
2/2/2016 10169
2/2/2016 1057
2/3/2016 10005
2/3/2016 10014

In file2 I add the actual date using this code.
Code:
awk -v date="$(date +"%m/%d/%Y")" '{print $0, date}' file1 > file2

file2
Code:
9/29/2010 10195 05/12/2017
9/29/2010 1057 05/12/2017
2/2/2016 10 05/12/2017
2/2/2016 10169 05/12/2017
2/3/2016 10005 05/12/2017
2/3/2016 10014 05/12/2017

then using this code i get the difference ( number of days) for 2 dates
Code:
awk -v q='"' 'BEGIN {OFS= ","}{
 c="date -d"q$1q" +%s";c|getline d1;close(c); 
 c="date -d"q$3q" +%s";c|getline d2;close(c);
 printf "%s, %s, %s, %d\n", $1, $2, $3,(d2-d1)/3600/24}' file2  > file3

file3 - Output file, Column 4 is the days between 2 dates.
Code:
9/29/2010, 10195, 05/12/2017, 2417
9/29/2010, 1057, 05/12/2017, 2417
2/2/2016, 10, 05/12/2017, 464
2/2/2016, 10169, 05/12/2017, 464
2/3/2016, 10005, 05/12/2017, 463
2/3/2016, 10014, 05/12/2017, 463

It works fine, BUT take a lot time to end.. My input file is a big file and it take very long time to end.

Kindly can you check the code and if it is possible modify it to be faster. Get same output (file3) but more faster.

Appreciate your help
# 2  
Old 05-12-2017
Do the links given at the bottom left under "More UNIX and Linux Forum Topics You Might Find Helpful" help you?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-12-2017
Hi RudiC.

Already I look in the More Unix and Linux Forum topics..

But nothing to help..Smilie
# 4  
Old 05-12-2017
No surprise it is taking its time, creating two processes per line for a large file. Try - provided you have a recent bash which you fail to mention -
Code:
date -f <(cut -d" " -f1 file2) +"($(printf "%(%s)T\n") - %s)/86400" | bc | paste file2 -
9/29/2010 10195 05/12/2017    2417
9/29/2010 1057 05/12/2017    2417
2/2/2016 10 05/12/2017    465
2/2/2016 10169 05/12/2017    465
2/3/2016 10005 05/12/2017    464
2/3/2016 10014 05/12/2017    464

This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-12-2017
Hi RudiC.

I got :
Code:
9/29/2010 10195 05/12/2017    -14880

# 6  
Old 05-12-2017
OK - what would you suggest? Looking at my sample output, comparing it to yours?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-12-2017
RudiC
The problem for me is here
Code:
date -f <(cut -d" " -f1 file2) +"($(printf "%(%s)T\n") - %s)/86400" | bc | paste file2 -

This value returns 0 for timestamp current date, here shoulbe value = 1494587900..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to get the sunday days between two dates?

Hi Am using Unix Ksh I have a two date input as DATE1=02/12/2012 DATE2=30/12/2012 I Need the output as only sunday date 02/12/2012 09/12/2012 16/12/2012 23/12/2012 30/12/2012 can anyone pls help me.. thanks in advance... (2 Replies)
Discussion started by: Venkatesh1
2 Replies

2. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

3. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

4. Shell Programming and Scripting

days are between the two dates?

I have two times in the format of YYMMDD. Does anyone know an easy way in ksh for me to display how many days are between the two dates? Example1: X=101202 Y=101205 There are 3 days between X & Y Example2: X=101202 Y=111202 There are 365 days between X & Y Example3: X=101205... (3 Replies)
Discussion started by: oldman2
3 Replies

5. Shell Programming and Scripting

Get number of days between given dates

Hi I need one single command to get number of days between two given dates.datecalc is not working. ex. fromdate:01.04.2010 todate :24.04.2010 i should get the out put as 23 Thanks in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

6. Shell Programming and Scripting

Days difference between two dates

Hello, I would like to find out the number of days between two dates of the format yyyy-mm-dd. Any help on this is highly appreciated. Thanks. (6 Replies)
Discussion started by: Data469
6 Replies

7. UNIX for Advanced & Expert Users

Number of days between two distinct dates

Hi I'm looking for a .ksh script/function that will calculate ONLY the number of days between two distinct dates. Further convert the number of days to weeks and display. I need this to be part of another larger script that checks the password expiry on several servers and notifies the... (1 Reply)
Discussion started by: radheymohan
1 Replies

8. Shell Programming and Scripting

Find number of days and list out the dates in between

Hi All, Can unix cshell list out the number of days between 070201 and 070205 (format is yymmdd) and list out all the dates in between in similiar format. set startdate = `date '+%y%m%d'` #eg 070201 set enddate = `date '+%y%m%d'` #eg 070205 i would expect the number of days to be 5... (2 Replies)
Discussion started by: Raynon
2 Replies

9. Shell Programming and Scripting

Difference between two dates in no of days ???

Hi All How to get the difference between two dates in no of days ??? My date format is like this YYYY/MM/DD. I have to get the no of days between two dates in the given format. I tried to search the forum but nothing came up similar to my requitement. Your help will be appreciated. ... (1 Reply)
Discussion started by: csaha
1 Replies

10. UNIX for Dummies Questions & Answers

days elapsed between 2 dates

does anybody know how to find out the number of days elapsed between 2 dates e.g. days elapsed between 020212 and 020110 (YYMMDD format) Thanking you in advance. Ravi. (1 Reply)
Discussion started by: rkkiran
1 Replies
Login or Register to Ask a Question