Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startOf('day')/endOf('day') not correct with UTC #1442

Closed
BCup opened this issue Jan 24, 2014 · 3 comments
Closed

startOf('day')/endOf('day') not correct with UTC #1442

BCup opened this issue Jan 24, 2014 · 3 comments

Comments

@BCup
Copy link

BCup commented Jan 24, 2014

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

@icambron
Copy link
Member

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?

@BCup
Copy link
Author

BCup commented Jan 25, 2014

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 ?

@icambron
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants