获取某年的某天是第几周

来源:互联网 发布:一千零一夜 知乎 编辑:程序博客网 时间:2024/05/07 13:37

方法一:

var date1 = new Date();var date2 = new Date(); date2.setMonth(0);date2.setDate(1); //当年第一天var rq = date1-date2;var s1 = Math.ceil(rq/(24*60*60*1000));var s2 = Math.ceil(s1/7);

方法二:

/** * 获取是否是闰年 */function isLeapYear(year) {    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);}/** * 获取第个月的天数 */function getMonthDays(year, month) {    return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);}/** * 获取某年的某天是第几周 */function getWeekNumber(y, m, d) {    var now = new Date(y, m - 1, d),        year = now.getFullYear(),        month = now.getMonth(),        days = now.getDate();    //那一天是那一年中的第多少天    for (var i = 0; i < month; i++) {        days += getMonthDays(year, i);    }    //那一年第一天是星期几    var yearFirstDay = new Date(year, 0, 1).getDay() || 7;    var week = null;    if(yearFirstDay < 7){    yearFirstDay = 7;    }    week = Math.ceil(days/7);    return week;}

/* 获取当月最后一天 */

new Date(2017,05,0);  //获取5月最后一天

new Date(2017,06,01);  //获取5月第一天


1 0