Please I am new to using moments
, and don't fully understand how utc
and isBefore
methods work.
I have the below code which returns false
, I need help understanding why even though logically it should be true since 2021-07-11
is before 2021-07-12
var moment = require('moment');
moment.utc('2021-07-11').isBefore('2021-07-12', 'day') // returns false
Any pointer to resource or why false is returned would be greatly appreciated.
What is your local timezone?
You are comparing local 12th, to UTC 11. If your timezone is in the western hemisphere, then this is the expected result due to the 'day'
parameter. When a timezone is not specified, momentJS will assume the local timezone.
The whole point of momentjs is to take timezones out of the equation for comparisons, so .isBefore()
will return true
only when the day component of the value passed in is less than the starting value when both values are transposed to the same timezone.
In this case, the day component of your two dates is equal and there before before will return false.
If your local timezone is UTC +2, then we can break your code down like this:
var utc = moment.utc('2021-07-11'):
var local = moment('2021-07-12');
// Local in UTC : 2021-07-11T22:00:00Z
utc.isBefore(local, 'day') // False
utc.isEqual(local, 'day') // True