Search code examples
coldfusioncoldfusion-9

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...


Solution

  • 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.