×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Get incorrect days between two dates in UNIX

Get incorrect days between two dates in UNIX

Get incorrect days between two dates in UNIX

(OP)
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

RE: Get incorrect days between two dates in UNIX

(OP)
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

RE: Get incorrect days between two dates in UNIX

(OP)
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

RE: Get incorrect days between two dates in UNIX

(OP)
I reviewed this Feb 2020 and it has 29 days (leap year). That could be the reason I got 83 days.

RE: Get incorrect days between two dates in UNIX

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 UTC'` - `date +%s -d '2019/12/22 UTC'`) / 86400)) days
84 days 

RE: Get incorrect days between two dates in UNIX

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

Thanks SamBones, very much appreciated!

RE: Get incorrect days between two dates in UNIX

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, https://www.timeanddate.com/time/zones/, 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 UTC'` - `date +%s -d '2019/12/22 UTC'`) / 86400)) days
84 days
$ echo $(((`date +%s -d '2020/03/15 MST'` - `date +%s -d '2019/12/22 MST'`) / 86400)) days
84 days
$ echo $(((`date +%s -d '2020/03/15 CST'` - `date +%s -d '2019/12/22 CST'`) / 86400)) days
84 days 

As long as you specify the same TZ on both dates, you should be ok.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close