Moment - a smartUI library to do date arithmetics
Moment.js is a library to extend the date and time values in Javascript by providing a wrapper around the date object. It is delivered as part of the smartUI.
Date-Formatting
Relative Time
Calendar Arithmetic
The library moment-with-locales.js (also included in smartUI supports every time and date format in the world.
Moment.js provides a wrapper for the native JavaScript date object. In doing this, Moment.js extends the functionality and also accounts for several deficiencies in the object. For example a day is added like
–moment('4/30/2016', 'MM/DD/YYYY').add(1, 'day')
//"2016-05-01T00:00:00-05:00"
The moment object in Moment.js is mutable. This means that operations like add, subtract, or set change the original moment object.
When first using Moment.js many developers are confused by scenarios like this:
var a = moment('2016-01-01');
var b = a.add(1, 'week');
a.format();
"2016-01-08T00:00:00-06:00"
Strict mode is the recommended mode for parsing dates. You should always use strict mode if your code base will allow it.
More than half of the parser issues seen on GitHub and Stack Overflow can be fixed by strict mode.
In a later release, the parser will default to using strict mode.
Strict mode requires the input to the moment to exactly match the specified format, including separators. Strict mode is set by passing true as the third parameter to the moment function.
moment('01/01/2016', 'MM/DD/YYYY', true).format()
"2016-01-01T00:00:00-06:00"
moment('01/01/2016 some text', 'MM/DD/YYYY', true).format()
"Invalid date"
Separator matching:
//forgiving mode
moment('01-01-2016', 'MM/DD/YYYY', false).format()
"2016-01-01T00:00:00-06:00"
//strict mode
moment('01-01-2016', 'MM/DD/YYYY', true).format()
"Invalid date"
Scenarios fixed by strict mode
//UUID matches YYYYDDD because it starts with 7 digits
moment('5917238b-33ff-f849-cd63-80f4c9b37d0c', moment.ISO_8601).format()
"5917-08-26T00:00:00-05:00"
//strict mode fails because trailing data exists
moment('5917238b-33ff-f849-cd63-80f4c9b37d0c', moment.ISO_8601, true).format()
"Invalid date“
//date has out of range value but is parsed anyways
moment('100110/09/2015', 'MM/DD/YYYY').format()
"2015-10-09T00:00:00-05:00"
//strict mode catches out of range issue
moment('100110/09/2015', 'MM/DD/YYYY', true).format()
"Invalid date“
//wrong date is parsed because strict mode ignores trailing data
moment('2016-12-31 11:32 PM').format('LT')
"11:32 AM"
//trailing data is noticed
moment('2016-12-31 11:32 PM', moment.ISO_8601, true).format('LT')
"Invalid date"
Forgiving Mode
While strict mode works better in most situations, forgiving mode can be very useful when the format of the string being passed to moment may vary.
A common scenario where forgiving mode is useful is in situations where a third party API is providing the date, and the date format for that API could change.
Suppose that an API starts by sending dates in 'YYYY-MM-DD' format, and then later changes to 'MM/DD/YYYY' format.
In strict mode, the following code results in 'Invalid Date' being displayed:
moment('01/12/2016', 'YYYY-MM-DD', true).format()
"Invalid date"
In forgiving mode using a format string, you get a wrong date:
moment('01/12/2016', 'YYYY-MM-DD').format()
"2001-12-20T00:00:00-06:00"
The wrong date scenario in forgiving mode is certainly less obvious to the user, but by that token could go unnoticed for a long time.
When choosing between strict and forgiving mode, it is important to consider whether it is more important that dates be accurate, or that dates never display as "Invalid Date".
Documentation
Homepage: momentjs.com
Documentation: momentjs.com/docs
Videos:
–Date and Time Odds, Ends and Oddities https://www.youtube.com/watch?v=ieIzNP6gKqU
Blogs
–Matt Johnson's Blog https://codeofmatt.com/
Misc
–Stack Overflow TimeZone Tag Wiki https://stackoverflow.com/tags/timezone/info
–Stack Overflow DateTime vs DateTimeOffset https://stackoverflow.com/q/4331189
–IANA Time Zone Database https://www.iana.org/time-zones