This question already has answers here:
Moment.js internal object what is "_d" vs "_i"
(2 answers)
Closed 6 years ago.
I'm using moment.js in my web application and encountered a bug where the time on many objects change to midnight at some point. I'm still trying to hunt down what, if anything triggers this change, but looking at one of the moments, I noticed the _i property shows the original / correct time.
Below is the developer console output of one of the moments in question, mm
_d: Thu Apr 02 2015 00:00:00 GMT-0400 (EDT)
_f: "YYYY-MM-DDTHH:mm:ssZ"
_i: "2015-04-02T12:10:43-04:00"
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale
_pf: Object
_tzm: -240
mm.toISOString()
// "2015-04-02T04:00:00.000Z"
moment(mm._i).toISOString()
// "2015-04-02T16:10:43.000Z"
What does the _i represent, and why doesn't it match the string output?
It turns out the bug was caused by a combination of 2 bad assumptions I had
calling startOf() mutates the moment, which I did not realize
the Immutable data structure I'm using ignores Objects with custom prototypes, so the moment objects were left mutable
The result was that another part of the application was able to mutate moments that I expected to be immutable
To answer my question though, I found the following
_i is the input when the moment object was originally created, and does not appear to change
_d is a Date object representing the date value after any mutations
mm = moment("2015-04-02T12:10:43-04:00")
mm._i // "2015-04-02T12:10:43-04:00"
mm._d // Thu Apr 02 2015 12:10:43 GMT-0400 (EDT)
mm.startOf('day')
mm._i // "2015-04-02T12:10:43-04:00"
mm._d // Thu Apr 02 2015 00:00:00 GMT-0400 (EDT)
Related
I am using in my database for Date and Time information the following format "yyyyMMddHHmmssSSS"
20211119143315000 and UTC. This corresponds to 19th November 2021 14:33:15 and 0 milliseconds in GMT+0.
The timezone is always UTC in the database. If my localtime in GMT 1 that example will be displayed as 19th November 2021 15:33:15 local time.
I use a DateTimePicker in my dialog and succeeded to display the correct local time with the following configuration in the XML View:
<DateTimePicker id="dp_lastUsedDate" valueFormat ="yyyyMMddHHmmssSSS Z" value="{path:'mdlPRTData>mp_last_used_date'}"/>
My issue is the format of the model value after I pick a date in the dialog. After I pick a date, the format of the value in the model is changed:
My actual local time is GMT+1
Original model value: 20211119143315000
Displayed value: 19.11.2021 15:33:15
Picked value: 19.11.2021 16:33:15
new model value: 20211119163315000 +0100
what I would expect: 20211119153315000
I could not find a solution (without making an individual change and formatter functions) for this requirement.ยด
Any tipps?
formatting a moment object gives me a different result to the one I expect to see
I tried removing UTC but still don't get the result i expect
moment.utc().startOf("day").subtract(schedule.pastDays, "days")
returns date object with:
_d: Wed Jul 17 2019 00:00:00 GMT+0000 (Etc Greenwich Standard Time) {}
but formatting it:
moment.utc().startOf("day").subtract(schedule.pastDays, "days").format()
returns:
"2019-07-16T22:00:00Z"
Where did the 2hrs go that kicked the date back to the previous day?
I expected to see:
"2019-07-17T00:00:00Z" as the date object would suggest.
So if I do not specify a timezone, moment presumes utc and so adjusts when I format(). This however works and keeps the formatted time local:
var tzDay = moment().utcOffset(moment().utcOffset(), true).local()
var newDay = tzDay.format('MMDDYYYY');
console.log(newDay)
//returns today's date without any utc adjustment
Don't understand why the examples on moment-timezone page give me different result than what they say they should.
Running these statements give me date with my current local time for each instance:
var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z'); // 5am PDT **I get 7am**
dec.tz('America/Los_Angeles').format('ha z'); // 4am PST **I get 6am**
What am I missing?
Found the issue was due to missing time zone data. After loading time zone data I get the expected results, below; how to load time zone data
[http://momentjs.com/timezone/docs/#/data-loading/][1]
I have this this date
<p am-time-ago="Thu Oct 15 21:53:55 IST 2015"></p>
it shows
[$parse:syntax] Syntax Error: Token 'Oct' is an unexpected token at column 5 of the expression [Thu Oct 15 21:53:55 IST 2015] starting at [Oct 15 21:53:55 IST 2015].
2015-10-13T18:49:52.888Z
it shows Token 'T18' is an unexpected token at column 11 of the expression [2015-10-13T18:49:52.888Z] starting at [T18:49:52.888Z].
Moment isn't parsing the date correctly because it is in an unsupported format.
For the first one, change IST to the offset (+05:30).
For the second, get rid of the T and Z, and put a space between the date and time.
Edit: Note that more recent versions of moment js don't support TZ abbreviations anymore, like IST or CST or UTC. That function was deprecated in 1.6.0.
Have a look at the year after setWeek
d = Date.today()
Wed Dec 26 2012 00:00:00 GMT+0000 (GMT Standard Time)
d.getWeek()
52
d.setWeek(52)
Mon Dec 23 2013 00:00:00 GMT+0000 (GMT Standard Time)
Anyone knows what this is about? Have I misinterpreted how the setWeek function should work?
I have found an issue in the datejs tracker with this same problem. It's from 2011, though it's still marked as new: setWeek issue
So regarding yor question of "Have I misinterpreted how the setWeek function should work?" I would say that it's a bug in the jdate code.
If you have a look at the datejs source code you will see that setWeek will always move to the Monday of the target week n:
Date.prototype.setWeek = function (n) {
return this.moveToDayOfWeek(1).addWeeks(n - this.getWeek());
};
see: datejs: svn/trunk/src/core.js