Parsing Nullish Date "0001-01-01T00:00:00Z" In Lucee CFML 126.96.36.199
At work, one of the APIs that I consume returns data for a Date column that didn't always exist. And, instead of returning a
null or an empty string for rows in which the value in the database is
NULL, they return what I assume is a null-coalesced date:
0001-01-01T00:00:00Z. I didn't realize that this was happening until I started to see dates in my system showing up as
2001 - which is many years before the API in question even existed. It turns out, passing a date/time mask into the
parseDateTime() function is kind of important in Lucee CFML 188.8.131.52.
To be honest, I almost never have to parse dates in my ColdFusion applications. So, I have next to no instinct for edge-cases around parsing dates from strings. When I tried the
parseDateTime() function with a "normal date" and without a date/time mask, it just worked. As such, I just assumed that I was good-to-go. But, there's something about the year
0001 that is mucking-up the parsing.
Luckily, I can fix this by explicitly providing the correct date/time mask:
<cfscript> // My computer runs in EST, but our production servers run in UTC, so I just want to // set my local code to run like the production code. setTimezone( "Etc/UTC" ); // This represents a "nullish" date returned by one of the APIs I consume. It's passed // over as a STRING, not a Date/Time value. nullishDate = "0001-01-01T00:00:00Z"; dump( nullishDate ); echo( "<hr />" ); // Attempt to parse the nullish date with no mask. dump( parseDateTime( nullishDate ) ); dump( year( parseDateTime( nullishDate ) ) ); echo( "<hr />" ); // Attempt to parse the nullish date with a DATE-only mask. dump( parseDateTime( nullishDate, "YYYY-MM-dd" ) ); dump( year( parseDateTime( nullishDate, "YYYY-MM-dd" ) ) ); echo( "<hr />" ); // Attempt to parse the nullish date with a full DATE/TIME mask. dump( parseDateTime( nullishDate, "YYYY-MM-dd'T'HH:nn:ss'Z'" ) ); dump( year( parseDateTime( nullishDate, "YYYY-MM-dd'T'HH:nn:ss'Z'" ) ) ); </cfscript>
In this code, I'm dumping-out the parsed date/time value as well as the
year() of the parsed value, just to make sure nothing funky is happening in the rendering. And, when we run this ColdFusion code, we get the following output:
As you can see, when omitting the mask, I get the incorrect year
2001. However, when including either a date-only mask or a full date/time mask, I get the correct year,
ASIDE: In my production code, I'm not actually worrying about this parsing issue. Instead, I just have this conditional check:
if ( date.left( 4 ) == "0001" )
... before I attempt to parse the value as a date.
So, I guess the real lesson here is just to always provide a date/time mask to the
parseDateTime() function even if the function appears to be working without it. Best not to leave anything to chance.
Want to use code from this post? Check out the license.
Completely off-topic, but I just want to mention how I LOVE that all of the code examples on your blog auto-expand horizontally on hover. I find the need to horizontally scroll to read code on 99% of blogs and articles infuriating. Thanks for going the extra UX mile here!
😊 Thank you so much! I'm super happy you like that. I'm slowly trying to make the user-experience on this site better, and that was one of the tweaks I made along the way!
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →