大家好,欢迎来到IT知识分享网。
判断某一天是该年中的第多少周,需要知道该年的第一天是星期几。
比如第一年第一天是星期一,那么直接获取那一天是该年中的第多少天,取它除以7的上限;
如果不是星期一,则需要进行如下计算:
一周是7天,减去那一天星期几,则该礼拜还有 7 – n天结束,再加一天则是下一礼拜。
示例代码:
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: }
完整demo代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<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'>
</head>
<body>
<script type="text/javascript">1:
2: /**3: * 判断年份是否为润年4: *5: * @param {Number} year6: */7: function isLeapYear(year) {8: return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);9: }
10: /**11: * 获取某一年份的某一月份的天数12: *13: * @param {Number} year14: * @param {Number} month15: */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>
<script type=”text/javascript”>
/**
* 判断年份是否为润年
*
* @param {Number} year
*/
function isLeapYear(year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
/**
* 获取某一年份的某一月份的天数
*
* @param {Number} year
* @param {Number} month
*/
function getMonthDays(year, month) {
return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}
function clickHandler() {
var y = $(‘year’).value,
m = $(‘month’).value,
d = $(‘day’).value;
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 == 1) {
week = Math.ceil(days/yearFirstDay);
} else {
days -= (7 – yearFirstDay + 1);
week = Math.ceil(days/7) + 1;
days = Math.max(days, 1);
}
alert(y + “年” + m + “月” + d + “日是” + year + “年的\n\n第” + days + “天\t第” + week + “周”);
}
</script>
选择日期:
<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>
<script type=”text/javascript”>
function $(id) {
return typeof id === ‘string’ ? document.getElementById(id) : id;
}
function addOptions(id, start, end) {
var opt = null,
frag = document.createDocumentFragment();
for (var i = start; i <= end ; i++) {
opt = document.createElement(“option”);
opt.value = i;
opt.innerHTML = i;
frag.appendChild(opt);
}
$(id).appendChild(frag);
}
function setDays(y, m) {
addOptions(‘day’, 1, getMonthDays(y, m – 1));
}
function changeDays() {
var val = $(‘day’).value;
$(‘day’).options.length = 0;
var y = $(‘year’).value,
m = $(‘month’).value;
setDays(y, m);
if (val) {
var maxDay = getMonthDays(y, m – 1);
$(‘day’).value = (val > maxDay) ? maxDay : val;
}
}
addOptions(‘year’, 1970, 2050);
addOptions(‘month’, 1, 12);
changeDays();
//默认设置为本地时间
!(function() {
var now = new Date();
$(‘year’).value = now.getFullYear();
$(‘month’).value = now.getMonth() + 1;
$(‘day’).value = now.getDate();
})();
$(‘year’).onchange = changeDays;
$(‘month’).onchange = changeDays;
document.getElementsByTagName(“button”)[0].onclick = clickHandler;
</script>
</body>
</html>
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/33898.html