Migrating from Moment.js to Luxon
In this article, I will mention some of the most commonly used features of moment.js and provide the luxon alternative of those features along with documentation links.
I must confess, I initially thought dayjs to be a better alternate to moment.js as the syntaxes are identical but after implementing it first hand, I personally found dayjs to be finicky and inconsistent. Some of the reasons being:
isValid()
incorrectly returns true for an invalid date when moment used to return false (probably fixed in recent versions).- Not able to parse both “3” and “03” to DD format consistently whereas moment could.
- Didn't like the way dayjs extends various plugins, looks odd to have 3+ LOC just to declare a library feature.
In spite of my initial reluctance for luxon, I quickly noticed how intuitive and convenient it was to migrate from moment.js. So, lets do this!
Parsing
This is one of my most commonly used feature and usually the first step if you are using a lot of user entered dates, which you would have to parse into a Date object to run further logic on them.