The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Number of days between two distinct dates radheymohan UNIX for Advanced & Expert Users 1 10-16-2007 08:39 AM
Find number of days and list out the dates in between Raynon Shell Programming and Scripting 2 02-12-2007 08:52 PM
Difference between two dates in no of days ??? csaha Shell Programming and Scripting 1 01-13-2006 06:44 AM
Difference between two dates... rajus19 Shell Programming and Scripting 4 01-04-2006 04:53 AM
days elapsed between 2 dates rkkiran UNIX for Dummies Questions & Answers 1 03-04-2002 12:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-10-2008
Data469 Data469 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 7
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 (permalink)  
Old 03-10-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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 (permalink)  
Old 03-11-2008
ali560045's Avatar
ali560045 ali560045 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 328
thanks a lot. this is really so helpfull to me...........i will never forget your help in this
  #4 (permalink)  
Old 03-11-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
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
  #5 (permalink)  
Old 03-26-2008
Data469 Data469 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 7
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 (permalink)  
Old 03-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Do you have Perl? [innocent glance]
  #7 (permalink)  
Old 03-26-2008
Data469 Data469 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 7
No I do not have Perl. This needs to be strictly in ksh.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0