Moment: startOf('day') is giving me yesterday

Created on 3 Apr 2016  ·  3Comments  ·  Source: moment/moment

Running the following code in Chrome Version 49.0.2623.110 m
image
Maybe related to #2982 ?

Most helpful comment

Because you are using the moment() function to construct your date, moment assumes you are handling a local date. Thus, the .startOf() function is moving you to the start of your local date.
.toISOString() though, outputs a UTC date, as indicated by the Z at the end of the strings.

Based on your github profile, it appears you are in Israel. So, adjusting for the Asia/Jerusalem timezone and putting it all in code:

//the time now in Jerusalem
moment().format()
"2016-04-03T14:48:10+03:00"

//start of day in Jersualem
moment().startOf('day').format()
"2016-04-03T00:00:00+03:00"

//start of day as ISO string is three hours before start of day in Jerusalem
moment().startOf('day').toISOString()
"2016-04-02T21:00:00.000Z"

You either need to work completely in local time, and use .format() to get the result you want, or work completely in UTC.

This section of the docs might be helpful:
http://momentjs.com/guides/#/parsing/local-utc-zone/

All 3 comments

Because you are using the moment() function to construct your date, moment assumes you are handling a local date. Thus, the .startOf() function is moving you to the start of your local date.
.toISOString() though, outputs a UTC date, as indicated by the Z at the end of the strings.

Based on your github profile, it appears you are in Israel. So, adjusting for the Asia/Jerusalem timezone and putting it all in code:

//the time now in Jerusalem
moment().format()
"2016-04-03T14:48:10+03:00"

//start of day in Jersualem
moment().startOf('day').format()
"2016-04-03T00:00:00+03:00"

//start of day as ISO string is three hours before start of day in Jerusalem
moment().startOf('day').toISOString()
"2016-04-02T21:00:00.000Z"

You either need to work completely in local time, and use .format() to get the result you want, or work completely in UTC.

This section of the docs might be helpful:
http://momentjs.com/guides/#/parsing/local-utc-zone/

:face-palm:

Thank you for the thorough explanation.

I think I've actually explained this one at least twice this week between here and Stack Overflow. You're not the only person to be confused. I'm going to update the docs to make the UTC part a bit more clear.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

chitgoks picture chitgoks  ·  3Comments

RobinvanderVliet picture RobinvanderVliet  ·  3Comments

alvarotrigo picture alvarotrigo  ·  3Comments

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments