Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I find the number of seconds till midnight of current day?

share|improve this question
Doens't that rather belong to stackoverflow? – Benoit Mar 16 '11 at 15:14
@Benoit : I considered that, but decided against because I thought that this would be scripting, not programming. – bguiz Mar 16 '11 at 23:02
Warning: you are trying to do interval arithmetic with absolute times of day. Think carefully about what happens around DST transitions. – Phil May 18 '11 at 14:00
@Phil : Yup, a solution which takes DST (and other corner cases) would be ideal – bguiz May 23 '11 at 13:02
up vote 6 down vote accepted

What about:

echo $(($(date -d "$(date +00:00-24:00)" +%s)-$(date +%s)))
share|improve this answer

If you have a version of date that supports it:

echo $(($(date -d 23:59:59 +%s) - $(date +%s) + 1))


echo $(($(date -d 'tomorrow 00:00:00' +%s) - $(date +%s)))
share|improve this answer
first one seems better from a computational efficiency perspective – Otheus Feb 5 at 11:52

If you want a solution which works always try this:

$((`date +%s` % 86400 + $OFFSET ))

The $OFFSET should be the difference in seconds between the local timezone and UTC.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.