Sunday, October 2, 2022
HomeJavaScriptTokenizing Date/Time Values In Lucee CFML 5.3.7.47

Tokenizing Date/Time Values In Lucee CFML 5.3.7.47


After my put up yesterday, on bucketing dates utilizing ground() in ColdFusion, James Moberg talked about on Twitter that he prefers to tokenize his dates utilizing the varied date components. This creates a human-friendly token versus the proprietary numeric illustration that I used to be utilizing in my put up. Utilizing the numeric illustration makes issues like looping tremendous straightforward; however, could make debugging a bit tougher. As such, I needed to take a second and take into consideration James’ strategy to tokenizing date/time values in Lucee CFML 5.3.7.47.

A couple of months in the past, I seemed as utilizing masks in ColdFusion’s parseDateTime() operate with the intention to deal with “nullish” dates popping out of a database. With parseDateTime(), the “masks” argument tells ColdFusion which string characters characterize which date/time parts. We will additionally use these identical masks values in ColdFusion’s formatDateTime() operate. What this implies is that we will use date/time masks to forged dates to-and-from a token.

If we wish to generate a “quick” token, with simply the date components, we will use the masks:

yyyymmdd

If we wish to generate a “lengthy” token, which incorporates each the date and time components, we will use the masks:

yyyymmddHHnnss

To see this in motion, let’s seize the present date, convert it to each forms of tokens, after which attempt to convert these tokens again into native ColdFusion dates:

<cfscript>

	immediately = dateConvert( "local2utc", now() );

	echo( "<h1> Tokenizing Date/Instances </h1>" );
	dump( immediately );

	// We will "tokenize" the date by making a easy, sortable string illustration of
	// the date-parts. The Quick token has day-level granularity, the lengthy token has
	// second-level granularity.
	shortToken = toShortToken( immediately );
	longToken = toLongToken( immediately );

	echo( "<h2> To Tokens </h2>" );
	dump( shortToken );
	dump( longToken );

	// We will then convert these tokens again into dates. For the reason that token is admittedly only a
	// "date masks", we will (internally) use the parseDateTime() to simply convert the
	// strings again into native ColdFusion dates.
	echo( "<h2> From Tokens </h2>" );
	dump( fromShortToken( shortToken ) );
	dump( fromLongToken( longToken ) );

	// ------------------------------------------------------------------------------ //
	// ------------------------------------------------------------------------------ //

	/**
	* I return an 8-character token for the given date.
	*/
	public string operate toShortToken( required date worth ) {

		return( dateTimeFormat( worth, "yyyymmdd" ) );

	}

	/**
	* I return a date for the given 8-character token.
	*/
	public date operate fromShortToken( required string token ) {

		return( parseDateTime( token, "yyyymmdd" ) );

	}

	/**
	* I return an 14-character token for the given date.
	*/
	public string operate toLongToken( required date worth ) {

		return( dateTimeFormat( worth, "yyyymmddHHnnss" ) );

	}

	/**
	* I return a date for the given 14-character token.
	*/
	public date operate fromLongToken( required string token ) {

		return( parseDateTime( token, "yyyymmddHHnnss" ) );

	}

</cfscript>

As you’ll be able to see, all we’re doing right here is utilizing ColdFusion’s built-in features, dateTimeFormat() to tokenize the date after which parseDateTime() to un-tokenize the date. And, after we run this Lucee CFML code, we get the next output:

Today's date being cast to a string token and then back to a native ColdFusion date.

As you’ll be able to see, by utilizing dateTimeFormat(), we will create easy, human-friendly tokens for our ColdFusion date/time values. On this case, the tokens are Strings; however, we might simply as simply have generated numeric tokens by wrapping our return values in val():

return( val( dateTimeFormat( .... ) ) );

I nonetheless consider that the numeric date illustration is the preferable manner for my put up yesterday on bucketing dates; however, that is primarily as a result of it makes the looping over date-ranges very easy. That stated, having the ability to use masks to transform back-and-forth between dates and tokens is a very nice function of ColdFusion. And, a way like that is particularly useful when persisting knowledge outdoors of the ColdFusion course of (corresponding to to a filename or a URL slug).

Need to use code from this put up?
Take a look at the license.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments