JS--计算某一年中在某一日所处的周数
<h2>計算週數</h2>
<input id="date" type="text" value="2019-03-19" />
<button onclick="Count($('#date').val())">計算</button>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
var firstStartWeek = 0; //得到今年1月1為週幾,星期日為起點 星期天=0
var year = ""; //年
var month = ""; //月
var day = ""; //日
var week = 0; //週數
var allDay = 0; //總天數
var monthDay = new Array();
function Count(val) {
year = val.substring(0, 4);
month = val.substring(5, 7);
day = val.substring(8, 10);
week = 0;
allDay = 0;
//減掉第一週剩餘的天數
firstStartWeek = 7 - new Date(year + "/01/01").getDay();
//計算潤平年
if ((parseInt(year) % 4 == 0 || parseInt(year) % 400 == 0) && (parseInt(year) % 100 != 0)) {
//閏年
monthDay = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
} else {
//平年
monthDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
}
if (firstStartWeek == 0) {
for (var i = 0; i < parseInt(month) - 1 ; i++) {
allDay += monthDay[i];
}
allDay += parseInt(day);//加上當前月的天數
week = Math.ceil(allDay / 7);
} else {
week++;//補回一周
for (var i = 0; i < parseInt(month) - 1 ; i++) {
allDay += monthDay[i];
}
allDay += parseInt(day);//加上當前月的天數
allDay -= firstStartWeek;//扣除第一周多餘的天數
week += Math.ceil(allDay / 7);
}
alert(week);
}
</script>
---------------------
作者:laizhixue
来源:CSDN
原文:https://blog.csdn.net/laizhixue/article/details/88889102
版权声明:本文为博主原创文章,转载请附上博文链接!