简体   繁体   English

如何在Javascript中将第一个工作日设置为星期一?

[英]How to set the 1st week day to Monday in Javascript?

I built an app totally based in dates.我构建了一个完全基于日期的应用程序。 Now the client really wants to make Monday as the 1st day of week and I've plenty of calendar views, diary view, log views, calculations, etc based in dates and always considering Sunday as first day of week because it's Javascript default.现在客户真的想把星期一作为一周的第一天,我有很多基于日期的日历视图、日记视图、日志视图、计算等,并且总是将星期日视为一周的第一天,因为它是 Javascript 的默认设置。 It's the day zero when you call the GetDay() function of the Date object.当您调用 Date 对象的 GetDay() 函数时,它是第 0 天。

Is there something I can do to set the 1st day of week as Monday ?有什么办法可以将一周的第一天设置为星期一吗?

ty !泰!

If you want monday as first day you can do this:如果你想星期一作为第一天,你可以这样做:

const dayIndex = defaultDayIndex === 0 ?常量 dayIndex = defaultDayIndex === 0 ? 6 : defaultDayIndex-1; 6 : defaultDayIndex-1;

const myDate = new Date('2021-12-07');

const defaultDayIndex = myDate.getDay();
// make monday first day of the week and sunday the last day
// monday will be index 0, and sunday will be index 6
const dayIndex = defaultDayIndex === 0 ? 6 : defaultDayIndex-1;
const weekMap = {
    1: 0, // → monday
    2: 1, // → tuesday
    3: 2, // → wednesday
    4: 3, // → thursday
    5: 4, // → friday
    6: 5, // → saturday
    0: 6, // → sunday
};

console.log(weekMap[(new Date()).getDay()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM