JavaScript:每年几周 [英] JavaScript: Weeks per year

查看:53
本文介绍了JavaScript:每年几周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,如何确定给定年份有几周?从31年12月31日获取星期数将失败,因为这可能会导致第1周.

In javascript, how can I find out how many weeks a given year has? Getting the weeknumber from year-dec-31 will fail since that can result in week 1.

此问题计算给定年份的周数可以回答这个问题,但是在JS中有什么巧妙的方法可以计算出来吗?

This question calculate number of weeks in a given year sort of answers it, but is there any neat way of calculating this in JS?

推荐答案

对于ISO 8601标准周

For the ISO 8601 Standard Weeks

function getISOWeeks(y) {
    var d,
        isLeap;

    d = new Date(y, 0, 1);
    isLeap = new Date(y, 1, 29).getMonth() === 1;

    //check for a Jan 1 that's a Thursday or a leap year that has a 
    //Wednesday jan 1. Otherwise it's 52
    return d.getDay() === 4 || isLeap && d.getDay() === 3 ? 53 : 52
}

我将以下两个帖子放在一起.

I put this together from the two following posts.

计算一年中的周数Ruby

javascript以查找leap年

这篇关于JavaScript:每年几周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆