A DEv
A DEv

Reputation: 340

How to get current time in seconds

Is there any easy way to get current time in seconds in CF9? Now I am achieving it by the following way.

 <cfset hs= hour(now())*60*60>
<cfset ms= minute(now())*60>
<cfset s= second(now())>
<cfset total_s =hs+ms+s>

thanks in advance...

Upvotes: 2

Views: 775

Answers (2)

Adam Cameron
Adam Cameron

Reputation: 29870

So the number of seconds from the most recent midnight?

function secondsFromBeginningOfDay(){
    var snapshotOfNow = now();
    var beginningOfDay = createDate(year(snapshotOfNow), month(snapshotOfNow), day(snapshotOfNow));
    var differenceInSeconds = dateDiff("s", beginningOfDay, snapshotOfNow);
    return differenceInSeconds;
}

It's important to not use now() any more than once in these operations, as a unit of time could reset between one call to now() and the next one, eg you start the process in the 59th second of a minute, and by the time you get to the seconds calculation, it's the next minute, so your figure will be 59s out. Also don't skimp on your variable and function names, but make it clear what each one is for.

Upvotes: 2

Ram Jadhav
Ram Jadhav

Reputation: 99

You may try timestamp. Timestamp gives us all details about time and then you can extract from it.

Upvotes: 0

Related Questions