sruthi
sruthi

Reputation: 21

Get the current date including milli-seconds

I have to track the current system time including milli-seconds using ColdFusion 11. I am using the Now() function but it outputs the date like this {ts '2017-01-11 06:48:58'}. I need to include the milli-seconds as well. Please let me know.

Upvotes: 1

Views: 2089

Answers (1)

Miguel-F
Miguel-F

Reputation: 13548

The milliseconds are there and you can get to them by using the TimeFormat() function of ColdFusion. Here is some sample code showing this:

<cfscript>
    currentTime = Now();
    writeOutput('<p>' & currentTime & '</p>');

    formattedTime = TimeFormat(currentTime,'HH:mm:ss.l');
    writeOutput('<p>' & formattedTime & '</p>');
</cfscript>

<!--- which outputs the following --->

{ts '2017-01-11 13:10:03'}

13:10:03.827

The first bit of code show the standard display format that you referenced. The second bit uses the TimeFormat() function to also include the milliseconds using the l mask option.

Here is a gist of that code so you can see it in action - TimeFormat example on trycf.com

There are several formatting options available to you using that function.

Masking characters that determine the format:

  • h: hours; no leading zero for single-digit hours (12-hour clock)
  • hh: hours; leading zero for single-digit hours (12-hour clock)
  • H: hours; no leading zero for single-digit hours (24-hour clock)
  • HH: hours; leading zero for single-digit hours (24-hour clock)
  • m: minutes; no leading zero for single-digit minutes
  • mm: minutes; a leading zero for single-digit minutes
  • s: seconds; no leading zero for single-digit seconds
  • ss: seconds; leading zero for single-digit seconds
  • l or L: milliseconds, with no leading zeros
  • t: one-character time marker string, such as A or P
  • tt: multiple-character time marker string, such as AM or PM
  • short: equivalent to h:mm tt
  • medium: equivalent to h:mm:ss tt
  • long: medium followed by three-letter time zone; as in, 2:34:55 PM EST
  • full: same as long

From the Adobe ColdFusion documentation here

If you still need the date portion of the object then use the DateFormat() function to display that part.

Upvotes: 4

Related Questions