每月通过Javascript获取周 [英] Get Weeks In Month Through Javascript

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

问题描述

在Javascript中,如何获取一个月的周数?我似乎找不到这个任何地方的代码。

In Javascript, how do I get the number of weeks in a month? I can't seem to find code for this anywhere.

我需要这个能够知道一个给定月份需要多少行。

I need this to be able to know how many rows I need for a given month.

更具体地说,我想要在一周中至少有一天的周数(一周定义为从星期日开始,到星期六结束)。

To be more specific, I would like the number of weeks that have at least one day in the week (a week being defined as starting on Sunday and ending on Saturday).

所以,对于这样的东西,我想知道它有5个星期:

So, for something like this, I would want to know it has 5 weeks:

S  M  T  W  R  F  S

         1  2  3  4

5  6  7  8  9  10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31 



Thanks for all the help.

推荐答案

这应该在星期天不开始工作。

This ought to work even when February doesn't start on Sunday.

function weekCount(year, month_number) {

    // month_number is in the range 1..12

    var firstOfMonth = new Date(year, month_number-1, 1);
    var lastOfMonth = new Date(year, month_number, 0);

    var used = firstOfMonth.getDay() + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}

这篇关于每月通过Javascript获取周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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