1

While writing Rounding time duration of difference to end of day to whole hours in momentjs?, I ran a few commands in Firefox console, and I just noticed this: when I specify a "naive" date, without any timezone, the ultimate Date recorded in the ._d property of the moment is one hour early:

var m1 = moment('2017-02-17 21:00:00');
<- undefined

m1
<- Object { _isAMomentObject: true, _i: "2017-02-17 21:00:00", \
_f: "YYYY-MM-DD HH:mm:ss", _isUTC: false, _pf: Object, _locale: Object, _a: Array[7], \
_d: Date 2017-02-17T20:00:00.000Z, \
_isValid: true, _z: null }

So, I request '2017-02-17 21:00:00', I get 2017-02-17T20:00:00.000Z recorded inside, in the ._d property. Now, I'm in CET, which is UTC+0100, so I guess moment.js assumes I enter a local datetime when I enter a "naive" one, and converts it to UTC when storing it in ._d.

That is OK, I guess, but what confused me is this in context of `endOf('day'):

var m2 = moment(m1).endOf('day');
<- undefined

m2
<- Object { _isAMomentObject: true, _i: "2017-02-17 21:00:00", \
_f: "YYYY-MM-DD HH:mm:ss", _isUTC: false, _pf: Object, _locale: Object, _z: null, _a: Array[7], \
_d: Date 2017-02-17T22:59:59.999Z, \
_isValid: true }

So I request end of day for "2017-02-17 21:00:00", and I get 2017-02-17T22:59:59.999Z, - that is, the time portion is "22:59:59", which I simply cannot see as end of day ?!

Then again, if I forget about this, the code I have that uses .endOf('day') (say, in the linked post) works as expected.

Could anyone explain the logic how come this works - even if 22:59:59 is not really the end of day?

sdaau
  • 36,975
  • 46
  • 198
  • 278
  • 4
    It seems to make sense to me. If `m1` represents a naive, *local* date and `_d` represents the UTC equivalent, then `m2` is the end of the *local* day (`23:59:59+1:00`), and its equivalent is in UTC (`22:59:59Z`) is stored in `_d`. Am I missing something? – p.s.w.g Dec 27 '18 at 23:32
  • 1
    The general idea is that everything is done relative to local time, but internally it stores the UTC time in `_d`. That's an implementation detail you shouldn't need to worry about. – Barmar Dec 28 '18 at 01:12

0 Answers0