Calculate the number of days between 2 dates - bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the number of days between 2 dates - bash script
# 1  
Old 03-23-2012
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?

Code:
#!/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!
# 2  
Old 03-23-2012
Looks alright. What is it that you want to know exactly?
# 3  
Old 03-23-2012
It doesn't need bash specifically, by the way.

What it does need is a modern version of GNU date, so if you take this script to a Solaris or AIX or other such system it won't work.
# 4  
Old 09-13-2012
a ksh solution

Code:
 
#/bin/ksh
 
# date format: yyyy-mm-dd
 
DATE1="2002-04-30"
DATE2="2006-12-02"
 
typeset -L4 y1 y2
typeset -Z -R2 mm dd
 
y1=$DATE1
y2=$DATE2
fy=$y1
my=$DATE1
my=${my#*-}
my=${my%-*}
 
c=0
d2=0
while [[ $y1 -le $y2 ]]
do
for mm in 1 2 3 4 5 6 7 8 9 10 11 12 do
[[ $y1 -eq $fy ]] && [[ $mm -lt $my ]] && continue cal $mm $y1 | while read line do
line=${line%%[a-zA-Z]*} [[ -n ${line:-""} ]] && {
for dd in $line do
(( c = c + 1 )) [[ "$y1-$mm-$dd" = "$DATE1" ]] && { d1=$c ; } [[ "$y1-$mm-$dd" = "$DATE2" ]] && { d2=$c ; break ; }
done
} [[ ${d2:-0} -gt 0 ]] && break
done [[ ${d2:-0} -gt 0 ]] && break
done (( y1 = y1 + 1 ))
done (( days = d2 - d1 )) echo $days


Last edited by rdrtx1; 09-14-2012 at 02:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 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.... (9 Replies)
Discussion started by: jiam912
9 Replies

2. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
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

Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example: $d1=2011 Oct 21 $d2=2012 Feb 20 I need to calculate the number of days between $d2 and $d1. This is on Solaris. Any ideas? Thanks, djanu (4 Replies)
Discussion started by: djanu
4 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

Calculate 30/31 days from today date script

Hi Guys, I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own... (3 Replies)
Discussion started by: coolgoose85
3 Replies

7. Shell Programming and Scripting

Script to calculate user's last login to check if > 90 days

I need a script to figure out if a user's last login was 90 days or older. OS=AIX 5.3, shell=Korn Here's what I have so far: ==== #!/usr/bin/ksh NOW=`lsuser -a time_last_login root | awk -F= '{ print $2 }'` (( LAST_LOGIN_TIME = 0 )) (( DIFF = $NOW - $LAST_LOGIN_TIME )) lsuser -a... (3 Replies)
Discussion started by: pdtak
3 Replies

8. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

9. 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

10. 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
Login or Register to Ask a Question