Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Days difference between two dates

Tags
date, days difference, difference, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-10-2008
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.
# 2  
Old 03-10-2008
try this to start - note you have to handle cross-years eg 2007 -> 2008
Code:
#!/bin/ksh
yday()
{
        perl -e '
                use POSIX qw(strftime);
                $fmt = "%j";  # %j day of year 1 - 366
                $mday = substr("$ARGV[0]", 8, 2);
                $mon =  substr("$ARGV[0]", 5 ,2);
                $year = substr("$ARGV[0]", 0 ,4);                  
                $weekday =
                  strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
                print int $weekday;
                ' "$1"
}

today=$( yday 2008-03-10 )
oldat=$( yday 2008-01-03 )
ddiff=$(( $today - $oldat ))
echo "$ddiff"

# 3  
Old 03-11-2008
thanks a lot. this is really so helpfull to me...........i will never forget your help in this
# 4  
Old 03-11-2008
A handy one :-)

Code:
$ cat daysdiff.sh
D1=`date +%s -d "2007-12-19"`
D2=`date +%s -d "2008-02-09"`
((diff_sec=D2-D1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'

//Jadu
The Following User Says Thank You to jaduks For This Useful Post:
figaro (08-21-2011)
# 5  
Old 03-26-2008
Thanks a lot for the replies.

But, I use HP-UX and it does not support date %s. Hence I need a different way to find the difference between the dates in a shell script.
# 6  
Old 03-26-2008
Do you have Perl? [innocent glance]
# 7  
Old 03-26-2008
No I do not have Perl. This needs to be strictly in ksh.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get number of days between 2 dates jiam912 Shell Programming and Scripting 9 05-16-2017 02:08 PM
Display previous days dates in ksh pr5439 Shell Programming and Scripting 5 12-22-2013 02:22 PM
Difference in dates in days (ksh shell) selvankj Shell Programming and Scripting 1 05-13-2013 11:31 AM
How to get the sunday days between two dates? Venkatesh1 UNIX for Advanced & Expert Users 2 12-18-2012 11:10 AM
Calculate the number of days between 2 dates - PHP kovacsakos Web Programming 3 03-23-2012 07:34 AM
Calculate days between yyyyMmmdd dates on Solaris djanu Shell Programming and Scripting 4 02-24-2012 05:52 PM
days are between the two dates? oldman2 Shell Programming and Scripting 3 12-02-2010 07:53 PM
Get number of days between given dates suryanarayana Shell Programming and Scripting 4 04-25-2010 09:43 AM
fetch dates for last 36 days in format yyyy-mm-dd pavan_test UNIX for Advanced & Expert Users 7 10-06-2009 04:14 AM
display all dates 200 days back pavan_test Shell Programming and Scripting 4 08-13-2009 12:01 PM
display all dates 200 days back pavan_test UNIX for Dummies Questions & Answers 1 08-12-2009 05:20 PM
using 'date' to get previous days' dates slant-40 UNIX for Dummies Questions & Answers 2 06-17-2008 09:49 PM
Number of days between two distinct dates radheymohan UNIX for Advanced & Expert Users 1 10-16-2007 09:39 AM
Difference between two dates in no of days ??? csaha Shell Programming and Scripting 1 01-13-2006 06:44 AM
days elapsed between 2 dates rkkiran UNIX for Dummies Questions & Answers 1 03-04-2002 12:45 PM


All times are GMT -4. The time now is 07:59 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password