Javascript 如何在javascript中确定日期是否是周末

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3551795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:08:36  来源:igfitidea点击:

how to determine if date is weekend in javascript

javascriptdate

提问by leora

if i have a date coming into a function, how can i tell if its a weekend day?

如果我有一个日期进入一个函数,我怎么知道它是否是周末?

回答by LukeH

var day = yourDateObject.getDay();
var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday

回答by kennebec

var isWeekend = yourDateObject.getDay()%6==0;

回答by user1949536

Short and sweet.

简短而甜蜜。

var isWeekend = ([0,6].indexOf(new Date().getDay()) != -1);

var isWeekend = ([0,6].indexOf(new Date().getDay()) != -1);

回答by T04435

I tried the Correct answer and it worked for certain locales but not for all:

我尝试了正确的答案,它适用于某些语言环境,但不适用于所有语言环境:

In momentjs Docs: weekdayThe number returned depends on the locale initialWeekDay, so Monday = 0 | Sunday = 6

在 momentjs 文档中:weekday返回的数字取决于 locale initialWeekDay,所以 Monday = 0 | 星期日 = 6

So I change the logic to check for the actual DayString('Sunday')

所以我改变逻辑来检查实际的 DayString('Sunday')

const weekday = momentObject.format('dddd'); // Monday ... Sunday
const isWeekend = weekday === 'Sunday' || weekday === 'Saturday';

This way you are Locale independent.

这样你就独立于语言环境。

回答by felippe

I've tested most of the answers here and there's always some issue with the Timezone, Locale, or when start of the week is either Sunday or Monday.

我已经在这里测试了大部分答案,但时区、语言环境或一周的开始时间是星期日或星期一总是存在一些问题。

Below is one which I find is more secure, since it relies on the nameof the weekday and on the enlocale.

下面是我发现更安全的一个,因为它依赖于工作日的名称en语言环境。

let startDate = start.clone(),
    endDate = end.clone();

let days = 0;
do {
    const weekday = startDate.locale('en').format('dddd'); // Monday ... Sunday
    if (weekday !== 'Sunday' && weekday !== 'Saturday') days++;
} while (startDate.add(1, 'days').diff(endDate) <= 0);

return days;

回答by Orlandster

Update 2020

2020 年更新

There are now multiple ways to achieve this.

现在有多种方法可以实现这一点。

1) Using the daymethod to get the days from 0-6:

1)使用day方法获取0-6天:

const day = yourDateObject.day();
// or const day = yourDateObject.get('day');
const isWeekend = (day === 6 || day === 0);    // 6 = Saturday, 0 = Sunday

2) Using the isoWeekdaymethod to get the days from 1-7:

2)使用isoWeekday方法获取1-7天:

const day = yourDateObject.isoWeekday();
// or const day = yourDateObject.get('isoWeekday');
const isWeekend = (day === 6 || day === 7);    // 6 = Saturday, 7 = Sunday

回答by Aaron Nusselbaum

In the current version, you should use

在当前版本中,您应该使用

    var day = yourDateObject.day();
    var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday

回答by Praveen

var d = new Date();
var n = d.getDay();
 if( n == 6 )
console.log("Its weekend!!");
else
console.log("Its not weekend");

回答by Muhammad Usama

Simply add 1 before modulo

只需在模数前加 1

var isWeekend = (yourDateObject.getDay() + 1) % 7 == 0;