I would like to understand Moment JS' initialisation of its moment object.
For example, if I would like to create a moment for the date and time: April 1 2000, 3:25:00 am with utc offset of +8 hours from UTC/GMT.
I represent this in a javascript object:
var obj = {
year: 2000,
month: 4, //APRIL
day: 1,
hour: 3,
minute: 25,
second: 0,
utcOffset: 8 //8 hours from UTC
}
I then create a handy function I can use to create a moment with moment js:
var makemoment = function(obj){
var m = moment([obj.year, obj.month - 1, obj.day, obj.hour, obj.minute, obj.second, 0]).utcOffset(obj.utcOffset);
return m;
}
When I use this function to create moment... e.g.
var result = moment(obj);
If I inspect the result object above I can see that it has a property _d
that I expect to have a value such as:
_d Date {Sat Apr 01 2000 03:25:00 GMT+0800 (HKT)}
But the actual value is which for me does not make sense since I specified that the time is 3:25:00 and it is already at GMT +0800 so no need to add 8 hours to the time....
_d Date {Sat Apr 01 2000 11:25:00 GMT+0800 (HKT)}
Despite of this _d value though, if I console.log(result)
I get the correct expected date:
2000-04-01T03:25:00+08:00
If I call the utc method on the moment. e.g. result.utc()
and if I inspect the object again, I can see that now the _d has changed to my originally expected value:
_d Date {Sat Apr 01 2000 03:25:00 GMT+0800 (HKT)}
However, now if I do result.format()
I get the correct UTC date and time:
2000-03-31T19:25:00+00:00
Am I not understanding something here???? How is the _d value used in Moment.js??? Should I ignore the _d value since it is just something internal to Moment.js???
I've created JSFiddle for to illustrate my point...