Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the week on Monday with isoWeekday()

People also ask

What is Isoweekday?

The function isoweekday() returns an integer value corresponding to the day of the week. Unlike the date. weekday() function the date. isoweekday() function returns the value 1 for Monday and increments it by one for the subsequent days.

How do I find the day of the week number from a moment?

$(document). ready(function(){ var weeknumber = moment("12-25-1995", "MM-DD-YYYY"). week(); console. log(weeknumber); });


You just need to replace begin.startOf('isoWeek'); with begin.startOf('week');.


This way you can set the initial day of the week.

moment.locale('en', {
    week: {
        dow: 6
    }
});
moment.locale('en');

Make sure to use it with moment().weekday(1); instead of moment.isoWeekday(1)


Call startOf before isoWeekday.

var begin = moment(date).startOf('week').isoWeekday(1);

Working demo


thought I would add this for any future peeps. It will always make sure that its monday if needed, can also be used to always ensure sunday. For me I always need monday, but local is dependant on the machine being used, and this is an easy fix:

var begin = moment().isoWeekday(1).startOf('week');
var begin2 = moment().startOf('week');
// could check to see if day 1 = Sunday  then add 1 day
// my mac on bst still treats day 1 as sunday    

var firstDay = moment().startOf('week').format('dddd') === 'Sunday' ?     
moment().startOf('week').add('d',1).format('dddd DD-MM-YYYY') : 
moment().startOf('week').format('dddd DD-MM-YYYY');

document.body.innerHTML = '<b>could be monday or sunday depending on client: </b><br />' + 
begin.format('dddd DD-MM-YYYY') + 
'<br /><br /> <b>should be monday:</b> <br>' + firstDay + 
'<br><br> <b>could also be sunday or monday </b><br> ' + 
begin2.format('dddd DD-MM-YYYY');