Select Page

DateGetTime() To Get UTC Milliseconds From Date In ColdFusion

Ben Nadel
Published: March 9, 2024

As of recent updates to Adobe ColdFusion 2021 / 2023, MySQL queries are no longer reporting dates as timestamps. For native ColdFusion logic, this isn’t much of a problem. However, in my ColdFusion applications, I’m often preparing data for consumption in JavaScript. And, in those cases, I serialize ColdFusion timestamps as UTC milliseconds using the .getTime() method on the underlying java.util.Date instance. But, now that the MySQL dates are coming back as strings, this .getTime() method isn’t available. As such, I wanted to create a more ColdFusion-native way to access this Epoch notation.

To be clear, ColdFusion is still built on top of Java; and, when I’m working with a native ColdFusion timestamp, it’s still built on top of the java.util.Date class. As such, in order to get the Epoch milliseconds from a date value, all we have to do is force ColdFusion to cast it to a native timestamp. And, to do that, all we have to do is pass it through a ColdFusion date/time function.

In the following code, I’ve created a User Defined Function (UDF), dateGetTime(), which takes an input, and either uses the native java.util.Date instance method; or, casts the value to a native timestamp and then uses the native java.util.Date instance method:

<cfscript>
	timestamp = now();
	// In the recent ColdFusion 2021/2023 updates, MySQL query dates are no longer coming
	// back from the database as timestamps - they are coming back as strings. To mock
	// this out, let's format our known valid timestamp as a string.
	mockDateFromDB = timestamp.dateTimeFormat( "yyyy-mm-dd HH:nn:ss" );
	// And, let's output the Epoch milliseconds for each.
	writeOutput("
		<p>
			From time-STAMP: #dateGetTime( timestamp )#
		</p>
		<p>
			From time-STRING: #dateGetTime( mockDateFromDB )#
		</p>
	");
	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //
	/**
	* I return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented
	* by this given date/time value.
	*/
	public numeric function dateGetTime( required any value ) {
		if ( isInstanceOf( value, "java.util.Date" ) ) {
			return( value.getTime() );
		}
		return( dateAdd( "d", 0, value ).getTime() );
	}
</cfscript>

As you can see, the magic sauce here is, dateAdd("d",0). By adding “nothing” to the given string representation of a date, ColdFusion with cast the result to a native java.util.Date instance (or rather, a subclass of it); and, at that point, we can access the .getTime() instance method. As such, when we run this Adobe ColdFusion code, we get the following output:

From time-STAMP: 1709982806634

From time-STRING: 1709982806000

The native timestamp – created via now() – has millisecond precision, which is why its Epoch milliseconds ends in 634. The dateAdd() casted timestamp is parsing a string that doesn’t contain a millisecond mask. As such, the subsequent date only as second precision, which is why it ends in 000. But, in both cases, we are able to extract the Epoch milliseconds.

Check out the license.


https://bennadel.com/go/4609

Source: www.bennadel.com