Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get incorrect days between two dates in UNIX 1

Status
Not open for further replies.

MikeDave888

Programmer
Feb 4, 2020
7
US
Hello everyone,

I am getting inconsistent days between two dates as below. Any idea? Any feedback would be greatly appreciated. Thank you everyone!

# This returns 42 days -- which is correct
> echo $(((`date +%s -d '2020/02/02'` - `date +%s -d '2019/12/22'`) / 86400)) days
42 days

# This returns 83 days -- which is incorrect. I am expecting 84 days
> echo $(((`date +%s -d '2020/03/15'` - `date +%s -d '2019/12/22'`) / 86400)) days
83 days
> echo $((($(date --date="2020/03/15" +%s) - $(date --date="2019/12/22" +%s)) / 86400)) days
83 days


Thanks and Regards,
Mike
 
The issue seems to be due to time zone change (1 hour forward) on March 8th 2020. But, I was testing them around 3pm and should not have any impact. I did some research online and some post suggested to add 43200 (which seems to be 12 hours). I am not sure if it is the right way to go. Any idea?

> echo $((($(date -d 2020-03-15 +%s) - $(date -d 2019-12-22 +%s) + 43200) / 86400))
84


Thanks and Regards,
Mike
 
This 43200 seems to make no much sense as it yields more incorrect results. Any feedback on how to resolve my above scenarios would be greatly appreciated. Thanks again!

Thanks and Regards,
Ken
 
I reviewed this Feb 2020 and it has 29 days (leap year). That could be the reason I got 83 days.
 
If you replace that divide (/) with a modulo (%), you'll see you're not looking at even days...

Code:
$ echo $(((`date +%s -d '2020/03/15'` - `date +%s -d '2019/12/22'`) % 86400)) 
82800

That looks to be off by 3,600 seconds, or one hour. Time zone is at play. Try setting it to UTC.

Code:
$ echo $(((`date +%s -d '2020/03/15 [b]UTC[/b]'` - `date +%s -d '2019/12/22 [b]UTC[/b]'`) / 86400)) days
84 days

 
Adding UTC worked for that scenario. I will run more scenarios and confirm.

Thanks SamBones, very much appreciated!
 
UTC isn't subject to daylight savings time, so it's like saying "just look at the day". If you're just looking to get the difference between days, and ignore any little plus or minus an hour, that's what UTC does.

Actually, I think you can use any time zone (i.e. Mountain Standard Time = "MST") and it will calculate the days correctly. As long as both dates have the same time zone code. I just like to use UTC because it's more "U"niversal.

If you go here, [URL unfurl="true"]https://www.timeanddate.com/time/zones/[/url], you'll notice that all global time zones are stated as an offset from UTC. UTC used to be called GMT, Grennich Mean Time.

Code:
$ echo $(((`date +%s -d '2020/03/15 [b]UTC[/b]'` - `date +%s -d '2019/12/22 [b]UTC[/b]'`) / 86400)) days
84 days
$ echo $(((`date +%s -d '2020/03/15 [b]MST[/b]'` - `date +%s -d '2019/12/22 [b]MST[/b]'`) / 86400)) days
84 days
$ echo $(((`date +%s -d '2020/03/15 [b]CST[/b]'` - `date +%s -d '2019/12/22 [b]CST[/b]'`) / 86400)) days
84 days

As long as you specify the same TZ on both dates, you should be ok.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top