Create real Dates from weird date-like strings using FlowBoost’s FBUtil.time (a.k.a Moment.js)

Today’s browsers create predictable Date objects from a wide range of date-like strings — from standard ISO 2023-12-08T12:34:56.000Z to nonstandard-but-reasonable variations like 12/8/2023 12:34.

That’s one reason people say “You don’t need a date library anymore.” See, it used to be that one browser understood a given format while another browser would barf. Or browsers would seem to understand, but create different Dates from the same string! So using a 3rd-party library was necessary, but not anymore.

FlowBoost still includes the trusted Moment.js library as FBUtil.time.

Why? Primarily because it’s simpler than using native Intl.DateTimeFormat – an API from the bowels of hell. But Moment.js can also deal with esoteric input formats that no browser will ever support.

For example, a Marketo Nation user mentioned she had a Marketo String field filled with bizarre — though at least consistent — values like this:

2023/12_08

That’s meant to signify December 8, 2023. One shudders at the back end that generates such stuff, and you can’t use the standard JS Date constructor on it: new Date("2023/12_08") creates an Invalid Date.

Moment.js can parse it, though. Just tell it the crazy format you have, with the Date and Time tokens in the right positions and any constants in single quotes:

myDate = FBUtil.time({{lead.Some DateTime Field}}, "YYYY'/'MM'_'DD");

Now you have a Moment object to do all kinds of things with, or simply pass it back to update a real Date/DateTime type field (by default, the response format is ISO yyyy-MM-dd'T'hh:MM:ss.zzzZ, which of course goes right into a DateTime):