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..
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..
Using only date and shell arithmetics:
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.But the following displays 0 in some locations:
Because of daylight savings time, there are only 23 hours between those times. You need to add at least one hour (and at most 23) to be safe.
Or you can tell
date
to work in a timezone without DST.(POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT+xx timezone.)
OSX
date
is different than GNUdate
. Got it working like this in OSX. This is not portable solution.Idea is still same as in the other answers. Convert dates to epoch time, subtract and convert result to days.
Gnu date knows %j to display the day in year:
crossing year-boundaries will give wrong results, but since you gave fixed dates ...
Got it
thanks..