首页 > 网站 > WEB开发 > 正文

获取某年的某天是第几周

2024-04-27 15:05:26
字体:
来源:转载
供稿:网友

方法一:

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;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表