Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to get difference between two dates

Tags:

date

shell

If there are dates as 2010-06-01 and another as 2010-05-15

Using shell script or date command how to get the number of days between the two dates

Thanks..

like image 581
Hulk Avatar asked Aug 02 '10 04:08

Hulk


People also ask

How do I get the difference between two dates in Shell?

There's a solution that almost works: use the %s date format of GNU date, which prints the number of seconds since 1970-01-01 00:00. These can be subtracted to find the time difference between two dates. Because of daylight savings time, there are only 23 hours between those times.


1 Answers

Using only date and shell arithmetics:

echo $((($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s")) / 86400))
like image 197
smichak Avatar answered Sep 22 '22 00:09

smichak