Moment: startOf('day')/endOf('day') not correct with UTC

Created on 24 Jan 2014  ·  3Comments  ·  Source: moment/moment

When using local time, the startOf('day')/endOf('day') function works as expected, but when i use UTC time the value is treated like local time.

http://jsfiddle.net/3d9eU/5/

moment.lang('nl');
var t1 = moment(); // localtime
var t2 = t1.clone().utc(); // UTC

console.log('LOC valueOf = ' + t1.valueOf()); //1390557395517 -> OK
console.log('UTC valueOf = ' + t2.valueOf()); // 1390557395517 -> OK

console.log('LOC end of day valueOf = ' + t1.endOf('day').valueOf()); //1390604399999 -> OK
console.log('UTC end of day valueOf = ' + t2.endOf('day').valueOf()); //1390607999999 -> NOT OK

console.log('LOC end of day _d = ' + t1.endOf('day')._d); //Fri Jan 24 2014 23:59:59 GMT+0100 (W. Europe Standard Time) -> OK
console.log('UTC end of day _d = ' + t2.endOf('day')._d); //Sat Jan 25 2014 00:59:59 GMT+0100 (W. Europe Standard Time) -> NOT OK

Best regards,
Björn

Troubleshooting

Most helpful comment

It sounds like you're asking why m.utc().endOf('day') gives you the end of day in UTC instead of in your local time. That's by design. utc() means "treat the time like it's UTC", where the end of the day is 23:59:59 _in UTC_, which is an hour later in western Europe. It's same as the way offset() works; once the moment is in a certain UTC offset, its operations work in that context.

In other words, these say very different things (I'm on the US east coast):

moment().utc().endOf('day').toString(); //=> "Sat Jan 25 2014 23:59:59 GMT+0000"
moment().endOf('day').utc().toString(); //=> "Sun Jan 26 2014 04:59:59 GMT+0000"

I hope that helps.

All 3 comments

What version of Moment is this? d is not a property on the Moment object (at least, anymore). There's a _d, which you should definitely not be referencing and which would be hugely misleading here. As for the actual issue, it works fine for me:

moment().utc().endOf('day').format(); //=> '2014-01-24T23:59:59+00:00'

So Im guessing you're on a prehistoric version?

I am using version 2.5.0.

The property '_d' is what i use to see what value Moment contains during debugging. (sorry for the misunderstanding). But why is this property different for local time vs. UTC? Both have timezone +0100 in stead of +0100 for local time and +0000 for UTC. see http://jsfiddle.net/3d9eU/5/

Local time: Fri Jan 24 2014 23:59:59 GMT+0100 (W. Europe Standard Time)
UTC: Sat Jan 25 2014 00:59:59 GMT+0100 (W. Europe Standard Time) shouldn't this be Fri Jan 24 2014 23:59:59 GMT+0100 (W. Europe Standard Time) with property _isUTC = true ?

It sounds like you're asking why m.utc().endOf('day') gives you the end of day in UTC instead of in your local time. That's by design. utc() means "treat the time like it's UTC", where the end of the day is 23:59:59 _in UTC_, which is an hour later in western Europe. It's same as the way offset() works; once the moment is in a certain UTC offset, its operations work in that context.

In other words, these say very different things (I'm on the US east coast):

moment().utc().endOf('day').toString(); //=> "Sat Jan 25 2014 23:59:59 GMT+0000"
moment().endOf('day').utc().toString(); //=> "Sun Jan 26 2014 04:59:59 GMT+0000"

I hope that helps.

Was this page helpful?
0 / 5 - 0 ratings