# 获取某一天是该年中的第多少周

```1: //那一年第一天是星期几

2: var yearFirstDay = new Date(year, 0, 1).getDay() || 7;

3:

4: var week = null;

5: if (yearFirstDay == 1) {

6:     week = Math.ceil(days/yearFirstDay);

7: } else {

8:     days -= (7 - yearFirstDay + 1);

9:     week = Math.ceil(days/7) + 1;

10:     days = Math.max(days, 1);

11: }```

```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>判定某一天在该年份中是第几周</title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv='content-type' content='text/html;charset=utf-8'>

<body>

<script type="text/javascript">   1:     2: /**   3:  * 判断年份是否为润年   4:  *    5:  * @param {Number} year   6:  */   7: function isLeapYear(year) {   8:     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);   9: }  10: /**  11:  * 获取某一年份的某一月份的天数  12:  *   13:  * @param {Number} year  14:  * @param {Number} month  15:  */  16: function getMonthDays(year, month) {  17:     return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);  18: }  19:    20:    21: function clickHandler() {  22:     var y = \$('year').value,  23:         m = \$('month').value,  24:         d = \$('day').value;  25:    26:     var now = new Date(y, m - 1, d),  27:         year = now.getFullYear(),  28:         month = now.getMonth(),  29:         days = now.getDate();  30:    31:     //那一天是那一年中的第多少天  32:     for (var i = 0; i < month; i++) {  33:         days += getMonthDays(year, i);  34:     }  35:       36:     //那一年第一天是星期几  37:     var yearFirstDay = new Date(year, 0, 1).getDay() || 7;  38:    39:     var week = null;  40:     if (yearFirstDay == 1) {  41:         week = Math.ceil(days/yearFirstDay);  42:     } else {  43:         days -= (7 - yearFirstDay + 1);  44:         week = Math.ceil(days/7) + 1;  45:    46:         days = Math.max(days, 1);  47:     }  48:    49:     alert(y + "年" + m + "月" + d + "日是" + year + "年的\n\n第" + days + "天\t第" + week + "周");  50: }</script>   1:     2:     3: 选择日期：   4: <select id="year"></select><label for="year">年</label>   5: <select id="month"></select><label for="month">月</label>   6: <select id="day"></select><label for="day">日</label>   7:     8: <button style='margin-left:30px;'>开始计算</button>   9:    10: <script type="text/javascript">  11: function \$(id) {  12:     return typeof id === 'string' ? document.getElementById(id) : id;  13: }  14:    15: function addOptions(id, start, end) {  16:     var opt = null,  17:         frag = document.createDocumentFragment();  18:           19:     for (var i = start; i <= end ; i++) {  20:         opt = document.createElement("option");  21:         opt.value = i;  22:         opt.innerHTML = i;  23:         frag.appendChild(opt);  24:     }  25:       26:     \$(id).appendChild(frag);  27: }  28:    29: function setDays(y, m) {  30:     addOptions('day', 1, getMonthDays(y, m - 1));  31: }  32:    33: function changeDays() {  34:     var val = \$('day').value;  35:       36:     \$('day').options.length = 0;  37:    38:     var y = \$('year').value,  39:         m = \$('month').value;  40:    41:     setDays(y, m);  42:    43:     if (val) {  44:         var maxDay = getMonthDays(y, m - 1);  45:    46:         \$('day').value = (val > maxDay) ? maxDay : val;  47:     }  48: }  49:    50: addOptions('year', 1970, 2050);  51: addOptions('month', 1, 12);  52: changeDays();  53:    54: //默认设置为本地时间  55: !(function() {  56:     var now = new Date();  57:     \$('year').value = now.getFullYear();  58:     \$('month').value = now.getMonth() + 1;  59:     \$('day').value = now.getDate();  60: })();  61:    62: \$('year').onchange = changeDays;  63: \$('month').onchange = changeDays;  64:    65: document.getElementsByTagName("button")[0].onclick = clickHandler;</script>

</body>
</html>```

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>判定某一天在该年份中是第几周</title> </head> <body> 选择日期： <select id="year"></select><label for="year">年</label> <select id="month"></select><label for="month">月</label> <select id="day"></select><label for="day">日</label> <button style='margin-left:30px;'>开始计算</button> </body> </html> 预览代码

413 篇文章24 人订阅

0 条评论