利用JS实现点击上一周或下一周却换
1.页面加载显示当前年份的第几周
效果如图:
html代码:
<font size="2" color="black">
<input id="btnweek5" type="button" class="btn" value="上周" onclick="EduCommission.GetLastWeek()" />
<span id="year"></span><span>第</span><span id="week"></span><span>周</span>
<input id="btnweek6" type="button" value="下周" class="btn" onclick="EduCommission.GetNextWeek()" />
</font>
JS代码:
var date = new Date();
nowyear = date.getFullYear();
nowweek = EduCommission.TheWeek(date);
$("#year").text(nowyear);
$("#week").text(nowweek - 1);
//本周 EduCommission.TheWeek = function (now) {
var totalDays = 0;
var years = now.getYear();
if (years < 1000)
years += 1900;
var days = new Array(12);
days[0] = 31;
days[2] = 31;
days[3] = 30;
days[4] = 31;
days[5] = 30;
days[6] = 31;
days[7] = 31;
days[8] = 30;
days[9] = 31;
days[10] = 30;
days[11] = 31;
if (Math.round(now.getYear() / 4) == now.getYear() / 4)
{ days[1] = 29; }
else { days[1] = 28;}
if (now.getMonth() == 0)
{
totalDays = totalDays + now.getDate();
}
else {
var curMonth = now.getMonth();
for (var count = 1; count <= curMonth; count++) {
totalDays = totalDays + days[count - 1]; }
totalDays = totalDays + now.getDate(); }
var week = Math.round(totalDays / 7) + 1;
return week; }
2.点击上一周或下一周时:
//点击上一周
EduCommission.GetLastWeek = function () {
var y = $("#year").text();
var week = $("#week").text();
if (week == 1) {
$("#year").text(y - 1);
var strdate = y - 1 + "-12-31";
var date = new Date(Date.parse(strdate.replace(/-/g, "/")));
var lastyearweek = EduCommission.TheWeek(date);
$("#week").text(lastyearweek); }
else {
$("#week").text(week - 1);
} };
//点击下一周
EduCommission.GetNextWeek = function () {
var y = $("#year").text();
var week = $("#week").text();
if (y < nowyear) {
var strdate = y + "-12-31";
var date = new Date(Date.parse(strdate.replace(/-/g, "/")));
var weekcount = EduCommission.TheWeek(date);
if (week == weekcount) {
$("#year").text(parseInt(y) + 1);
$("#week").text(1);
}
else {
$("#week").text(parseInt(week) + 1);
}
else {
$("#week").text(parseInt(week) + 1);
}
};