I'm using moment.js and I'm doing the following:
moment().startOf('isoWeek').day(0)
Instead of giving me the Sunday for this week - 25th, it returns the previous Sunday, 18th.
I have days from 0-6 saved in the database (1 = Monday, 0 = Sunday)
How can I use 0 to get this week's Sunday and not the previous Sunday?
Edit: Thanks for all the replies. I think I'll just add a week to the date for now but longer term will probably switch over to use date-fns
across the project. Thanks.
While startOf('isoWeek')
uses Monday as the first day of the week, .day(0)
uses the ECMAScript definition and takes you back another day (because based on its definition the Sunday before the Monday belongs to the respective week).
To work around it and get the upcoming Sunday, add another week using moment's add
function as described on https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/01-add/:
moment().startOf('isoWeek').day(0).add(1, 'weeks')
or use the shorthand as suggested on https://momentjscom.readthedocs.io/en/latest/moment/02-get-set/06-day/:
moment().startOf('isoWeek').day(7);