I want to ask your that how to determine & convert current time to no of seconds elapsed during current day?

Actually, I want to show how long is midnight from now in seconds. As total seconds in a day is 86400 so I'll subtract current time (in seconds elapsed since this day) with 86400.


Here is the actual question. If date -d is not working then how can our instructor give question like this. There might be a way maybe. Anyone understand this question. They have a different way that is subract from no of seconds.

enter image description here

  • 2
    Related: How do I find seconds since midnight? Jul 16 '16 at 1:53
  • Hey @steeldriver can you tell me is there any other way than -d parameter in date command to calculate no of seconds until midnight. Solution below is great, but -d is ot working. Thanks
    – Zeus
    Jul 16 '16 at 2:40
  • Sorry I'm not familiar with other implementations of the date command - for a more platform-independent solution you might want to look at languages that provide their own datetime functions, such as perl Jul 16 '16 at 2:45
  • @steeldriver Thanks can you please help me. I have posted the actual question PIC also. There might be a way or a hint in the question if date -d doesn't work. Any idea?
    – Zeus
    Jul 16 '16 at 2:50
  • Well, you can trivially get the elapsed seconds since 00:00:00 today using date with respectively +%H, +%M, +%S and basic arithmetic (in the syntax appropriate for the shell of your instructor's choice). Then - assuming you aren't expected to account for the possibility of leap seconds occurring before tomorrow - simple subtraction will give you the number of seconds remaining until next 00:00:00 Jul 16 '16 at 3:33

Assuming you have GNU date (or another date program that understands -d and works properly) and using bash:

You can use the -d flag to report time at various points. So, for example

% date -d "00:00:00 tomorrow"
Sat Jul 16 00:00:00 EDT 2016

We can use this to report on seconds with +%s

% date -d "00:00:00 tomorrow" +%s

We know our current time, in seconds:

% date +%s                       

So we can calculate the difference:

% secs_left=$(( $(date -d "00:00:00 tomorrow" +%s) - $(date +%s) ))
% echo $secs_left

(Which is about right; 7871 seconds is 2hrs 11mins and 11 seconds, which is correct for when I ran that command).

EDIT for csh as requested:

In csh the similar command would be

% @ secs_left = ( `date -d "00:00:00 tomorrow" +%s` - `date +%s` )
% echo $secs_left
  • Hey bro, really sorry to tell you I am bound to use Csh. Can you please tell in that. Thanks
    – Zeus
    Jul 16 '16 at 2:05
  • csh commands added, but I really don't recommend you write code in csh - see www-uxsup.csx.cam.ac.uk/misc/csh.html for reasons. Long term you'll be grateful to learn bash syntax! Jul 16 '16 at 2:12
  • Yes! I agree. it was our instructor's choice. And is there any other way than -d parameter? In Gator date works date -u also works but not date -d. Thanks
    – Zeus
    Jul 16 '16 at 2:14
  • Although it didn't helped me much as I'm still stucked in date -d error , but your asnwer is definitely great. So, you deserve accept asnwer. Thanks . If possilbe kindly help
    – Zeus
    Jul 16 '16 at 3:22
  • Your instructor is expecting you to call date with +%H to get the current hours, with %+M to get current minutes and %+S to get seconds. From there you can calculate (similar to the @ line in my answer) the number of seconds from midnight. At this point you have all the information you need to do the calculation. Jul 16 '16 at 3:47


eval "`date +'@ s = (86400 - %S) - 60 * (%M + 60 * %H)'`"; echo $s

However note that in timezones that have winter and summer time, it won't give the right result if called on the day of the switch from/to summer time, before the switch (which generally happen very early in the morning).

Beware that in csh, arithmetic operators are right-associative with */ having precedence over +-, as in

@ s = 1 - 2 + 3 - 4


@ s = 1 - (2 + (3 - 4))

And not:

@ s = (((1 - 2) + 3) - 4)

as in other languages. That was fixed in tcsh (6.15.01), and you can run set compat_expr to get back to the older behaviour there.

Hence the parentheses around 86400 - %S so it works in both csh and tcsh.

  • yo man! it worked :D :D :D Thanks man. The other solution were great, but this one worked for me so I changed my accept answer and also they got their upvotes so I hope they won't mind. But I didn't get what you said about timezones. I live in Vancouver, British Columbia, Canada with Pacific Standard Time.
    – Zeus
    Jul 16 '16 at 16:27
  • @Tony, in Vancouver, 2016-11-06 is a 25 hour long day for instance. At 02:00, the clock goes back to 01:00 again to switch to winter time which adds an extra hour. If you run that command at 00:30 before the switch, that's 24.5 hours before midnight, but that command will report 23.5 hours because it calculates 24 hours take away the current time. Jul 16 '16 at 17:09
  • At that time our course would have been finished :D, but thanks a lot for this information.
    – Zeus
    Jul 16 '16 at 17:17
  • Dear Stephane, could you please help me in this question? You are a genius. Hope you don't mind. unix.stackexchange.com/questions/296372/…
    – Zeus
    Jul 17 '16 at 0:21

in csh it is pretty simple:

    set start_time = `date +%s`
    set end_time = `date +%s`
    @ time_elapsed = $end_time - $start_time
    echo "$time_elapsed time elapsed in seconds"
    @ time_elapsed = `date -u -r $time_elapsed +%T`
    echo = "$time_elapsed time elapsed in hh:mm:ss format"

this reads like a homework question.

given that you are constrained to only getting hours minutes and seconds you will have to take those numbers and do arithmentic. the arithmetic is not complex, I'm sure you can work it out.

this will not give a reliable result because there is a race condition if the minute changes during the determination of those three input values so steps should be taken to detect this fault and correct for it.

use the result form @StevenHaris' answer to confirm your calculations are correct,

  • One can always use eval `date '+h=%H m=%M s=%S'` to get all three values at once. Jul 16 '16 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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