代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

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

  •  62
  • leora Matt Lacey  · 技术社区  · 14 年前

    如果我有一个日期进入一个功能,我怎么知道这是一个周末?

    6 回复  |  直到 14 年前
        1
  •  167
  •   Pavel P    4 年前
    var dayOfWeek = yourDateObject.getDay();
    var isWeekend = (dayOfWeek === 6) || (dayOfWeek  === 0); // 6 = Saturday, 0 = Sunday
    
        2
  •  50
  •   kennebec    14 年前
    var isWeekend = yourDateObject.getDay()%6==0;
    
        3
  •  10
  •   user1949536    10 年前

    又短又甜。

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

        4
  •  5
  •   T04435    6 年前

    在momentjs文档中: weekday 返回的数字取决于区域设置initialWeekDay,因此Monday=0 | Sunday=6

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

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

    这样您就可以独立于区域设置。

        5
  •  1
  •   Orlandster BigMac    5 年前

    1) 使用 day 获取0-6之间天数的方法:

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

    isoWeekday 获取1-7天的方法:

    const day = yourDateObject.isoWeekday();
    // or const day = yourDateObject.get('isoWeekday');
    const isWeekend = (day === 6 || day === 7);    // 6 = Saturday, 7 = Sunday
    
        6
  •  0
  •   Praveen    9 年前
    var d = new Date();
    var n = d.getDay();
     if( n == 6 )
    console.log("Its weekend!!");
    else
    console.log("Its not weekend");
    
        7
  •  0
  •   felippe    5 年前

    下面是一个我发现更安全的,因为它依赖于 名称 在工作日和 区域设置。

    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;
    
        8
  •  0
  •   Aaron Nusselbaum    5 年前

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

        var day = yourDateObject.day();
        var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday
    
        9
  •  0
  •   kibe    4 年前

    对Date对象使用.getDay()方法来获取日期。

    var givenDate = new Date('2020-07-11');
    var day = givenDate.getDay();
    var isWeekend = (day === 6) || (day === 0) ? 'It's weekend': 'It's working day';
        
    console.log(isWeekend);
    
        10
  •  -1
  •   NVRM    4 年前

    以下输出一个布尔值,表示日期对象是否在 开放 小时数,不包括周末和夜间 23H00 9H00 ,同时考虑客户端时区偏移。

    当然,这不适用于特殊情况,如节假日,但不远;)

    let t = new Date(Date.now()) // Example Date object
    let zoneshift = t.getTimezoneOffset() / 60
    let isopen = ([0,6].indexOf(t.getUTCDay()) === -1) && (23 + zoneshift  < t.getUTCHours() === t.getUTCHours() < 9 + zoneshift)
    
    // Are we open?
    console.log(isopen)
    <b>We are open all days between 9am and 11pm.<br>
    Closing the weekend.</b><br><hr>
    
    Are we open now?

    或者,将一周中的某一天作为区域设置 字符串,我们可以使用:

    let t = new Date(Date.now()) // Example Date object
    
    console.log(
      new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(t) ,
      new Intl.DateTimeFormat('fr-FR', { weekday: 'long'}).format(t) ,
      new Intl.DateTimeFormat('ru-RU', { weekday: 'long'}).format(t)
    )

    当心 new Intl.DateTimeFormat 如果在循环中运行缓慢,则简单关联数组的运行速度要快得多:

    console.log(
      ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date(Date.now()).getDay()]
    )
        11
  •  -3
  •   Muhammad Usama    10 年前

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