How to get seconds elapsed since midnight

Asked
Active3 hr before
Viewed126 times

6 Answers

elapsedseconds
90%

Multiply the current hour by the number of seconds in each hour and add it to the number of minutes multiplied by the number of seconds in each minute. ,Simplest I believe would be dividing the current time (in seconds) by the number of seconds in a day (60*60*24) and taking the remainder:,Using PHP how do you get the number of seconds elapsed since midnight of the current day?,Based on your comment, if you are receiving the time as a string and want to calculate the number of seconds from that time:

This should work.

echo time() - strtotime("today");
load more v
88%

I'd rather use something along these lines: IFS=: read -r today now <<< $(date +%F:%s); midnight=$(date -d "$today 0" +%s); echo $(( now - midnight )) – x-yuri May 3 '18 at 20:41 ,To avoid race conditions, still assuming GNU date:,Using the date program, how can I calculate the number of seconds since midnight?,Similarly, I find it useful to have a function for minutes since midnight:

To avoid race conditions, still assuming GNU date:

eval "$(date +'today=%F now=%s')"
midnight = $(date - d "$today 0" + % s)
echo "$((now - midnight))"

With zsh, you can do it internally:

zmodload zsh / datetime
now = $EPOCHSECONDS
strftime - s today % F $now
strftime - rs midnight % F $today
echo $((now - midnight))

Portably, in timezones where there's no daylight saving switch, you could do:

eval "$(date +'h=%H m=%M s=%S')"
echo "$((${h#0} * 3600 + ${m#0} * 60 + ${s#0}))"
load more v
72%

How can I get the number of seconds since midnight from CURRENT TIME or TIMESTAMP? Is there a function to manipulate/edit the timestamp?,By using the new function in 15.1 called TimeStampDiff (abbreviated TSD here), you can easily get this value. I have given a sample query below so you can see everything pretty clearly.,To learn more about the TSD function, please see the Version 15.1 documentation for Scalar Functions. In addition to this, there is a Knowledge Base article I would recommend for you. Please read through Knowledge Base article 212993, titled "Using the CA Datacom/DB SQL function TIMESTAMPDIFF to compute dates with rounded results" when you have a chance, to find more uses for the TSD function.,As always, please contact Broadcom support for CA Datacom if you have further questions.

Now, without further ado, here is the TSD-based query:

//SQL0001 EXEC @SQL,PARM='INPUTWIDTH=80,PRTWIDTH=150'//SYSIN   DD  *,SYMBOLS=JCLONLY                                               *$COLUMN                                                                      -- ********************************************************************       -- ** 9. TSD: Number of seconds since midnight                       **       -- ********************************************************************                                                                                     Select  '9. Num seconds since midnight'                           As Title    -- ---------------------------------------------------------------------------  ,Current Timestamp                                              As Now        ,Current Date                                                   As Dateonly -- ----------------------------------------------------------------------------- Display TimeStampDiff in truncated Seconds and Microseconds-- TimeStampDiff will set the time to midnight (00:00:00.000000) if empty  ,TimeStampDiff(SECOND,     Current Date, Current Timestamp)     As Second     ,TimeStampDiff(MICROSECOND,Current Date, Current Timestamp)     As Microsec -- ----------------------------------------------------------------------------- Round seconds using microseconds and adding half a second of Microseconds     -- Then divide by a full second's microsecs                                     ,DEC((TimeStampDiff(MICROSECOND,Current Date, Current Timestamp)                                 + 500000) / 1000000  ,5,0)                   As BySecond -- --------------------------------------------------------------------------- From SYSADM.MUF_Systems_Stats MSS;                                           /*                                                        
65%

Thanks to everyone who sent me corrections and updates!,More date related programming examples: What's the current week number? - What's the current day number?,Please note: All tools on this page are based on the date & time settings of your computer and use JavaScript to convert times. Some browsers use the current DST (Daylight Saving Time) rules for all dates in history. JavaScript does not support leap seconds.

load more v
75%

var todayDate = new Date();
var Todaymilli = todayDate.getTime();
var todayMin = todayDate.getMinutes();
var diff = Todaymilli - todayMin;
var diffMin = diff/(1000*60*60);
var accurateMin = Math.floor(diffMin);


document.write("Current Date: " + todayDate);
document.write("<br> Elapsed milliseconds since midnight, Jan 1, 1970: " + Todaymilli);
document.write("<br> Elapsed minutes since midnight, Jan 1, 1970: " + accurateMin)
40%

The events that these celebrate are typically described as "N seconds since the Unix epoch", but this is inaccurate; as discussed above, due to the handling of leap seconds in Unix time the number of seconds elapsed since the Unix epoch is slightly greater than the Unix time number for times later than the epoch. ,A Unix clock is often implemented with a different type of positive leap second handling associated with the Network Time Protocol (NTP). This yields a system that does not conform to the POSIX standard. See the section below concerning NTP for details. ,chrono-Compatible Low-Level Date Algorithms – algorithms to convert between Gregorian and Julian dates and the number of days since the start of Unix time,All articles containing potentially dated statements

Unix time (also known as Epoch time, Posix time,[1] seconds since the Epoch,[2] or UNIX Epoch time[3]) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970 (an arbitrary date). Unix time is nonlinear with a leap second having the same Unix time as the second before it (or after it, implementation dependent), so that every day is treated as if it contains exactly 86400 seconds,[2] with no seconds added to or subtracted from the day as a result of positive or negative leap seconds. Due to this treatment of leap seconds, Unix time is not a true representation of UTC.

Unix time is widely used in operating systems and file formats. In Unix-like operating systems, date is a command which will print or set the current time; by default, it prints or sets the time in the system time zone, but with the -u flag, it prints or sets the time in UTC and, with the TZ environment variable set to refer to a particular time zone, prints or sets the time in that time zone.[4]

date

Unix time (also known as Epoch time, Posix time,[1] seconds since the Epoch,[2] or UNIX Epoch time[3]) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970 (an arbitrary date). Unix time is nonlinear with a leap second having the same Unix time as the second before it (or after it, implementation dependent), so that every day is treated as if it contains exactly 86400 seconds,[2] with no seconds added to or subtracted from the day as a result of positive or negative leap seconds. Due to this treatment of leap seconds, Unix time is not a true representation of UTC.

Unix time is widely used in operating systems and file formats. In Unix-like operating systems, date is a command which will print or set the current time; by default, it prints or sets the time in the system time zone, but with the -u flag, it prints or sets the time in UTC and, with the TZ environment variable set to refer to a particular time zone, prints or sets the time in that time zone.[4]

-u

Unix time (also known as Epoch time, Posix time,[1] seconds since the Epoch,[2] or UNIX Epoch time[3]) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970 (an arbitrary date). Unix time is nonlinear with a leap second having the same Unix time as the second before it (or after it, implementation dependent), so that every day is treated as if it contains exactly 86400 seconds,[2] with no seconds added to or subtracted from the day as a result of positive or negative leap seconds. Due to this treatment of leap seconds, Unix time is not a true representation of UTC.

Unix time is widely used in operating systems and file formats. In Unix-like operating systems, date is a command which will print or set the current time; by default, it prints or sets the time in the system time zone, but with the -u flag, it prints or sets the time in UTC and, with the TZ environment variable set to refer to a particular time zone, prints or sets the time in that time zone.[4]

TZ
load more v

Other "elapsed-seconds" queries related to "How to get seconds elapsed since midnight"