Home > Web Front-end > JS Tutorial > JS operation date, by the way, realize the previous week and next week functions_time and date

JS operation date, by the way, realize the previous week and next week functions_time and date

WBOY
Release: 2016-05-16 18:43:48
Original
1363 people have browsed it

Today, a working student asked me for help:
1. When the page loads, the current date is displayed in one place, with an arrow on the left and right of it, indicating the previous week and the next week respectively.
The table below displays each day of the week. Date, from Monday to Sunday
2. Click the left and right arrows, that place will display today’s day of the previous week or next week, and the table below will display each date of the previous week or next week

JS itself does not Provides the ability to add and subtract dates, so I wrote several methods to operate dates, mainly to add and subtract dates
Author: Chenzhou Tuozhi Ren Wenmin

jsDate.htm


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> var currDT; var aryDay = new Array("日","一","二","三","四","五","六"); //初始页面 function ini() { currDT = new Date(); showDate(); } //上一周 或 下一周 function addWeek(ope) { var num = 0; if(ope=="-") { num = -7; } else if(ope=="+") { num = 7; } currDT = addDate(currDT,num); showDate(); } function showDate() { span1.innerHTML = currDT.toLocaleDateString(); //显示日期 var dw = currDT.getDay(); var tdDT; //确定周一是那天 if(dw==0) { tdDT = addDate(currDT,-6); } else { tdDT = addDate(currDT,(1-dw)); } //在表格中显示一周的日期 var objTB = document.getElementById("tb1"); for(var i=0;i<7;i++) { if(tdDT.toLocaleDateString()==currDT.toLocaleDateString()) { objTB.rows[0].cells[i].style.color = "red"; //currDT突出显示 } dw = tdDT.getDay(); objTB.rows[0].cells[i].innerHTML = tdDT.getMonth()+1 + "月" + tdDT.getDate() + "日 星期" + aryDay[dw]; tdDT = addDate(tdDT,1); //下一天 } } //增加或减少若干天,由 num 的正负决定,正为加,负为减 function addDate(dt,num) { var ope = "+"; if(num<0) { ope = "-"; } var reDT = dt; for(var i=0;i<Math.abs(num);i++) { reDT = addOneDay(reDT,ope); } return reDT; } //增加或减少一天,由ope决定, + 为加,- 为减,否则不动 function addOneDay(dt,ope) { var num = 0; if(ope=="-") { num = -1; } else if(ope=="+") { num = 1; } var y = dt.getYear(); var m = dt.getMonth(); var lastDay = getLastDay(y,m); var d = dt.getDate(); d += num; if(d<1) { m--; if(m<0) { y--; m = 11; } d = getLastDay(y,m); } else if(d>lastDay) { m++; if(m>11) { y++; m = 0; } d = 1; } var reDT = new Date(); reDT.setYear(y); reDT.setMonth(m); reDT.setDate(d); return reDT; } //是否为闰年 function isLeapYear(y) { var isLeap = false; if(y%4==0 && y%100!=0 || y%400==0) { isLeap = true; } return isLeap; } //每月最后一天 function getLastDay(y,m) { var lastDay = 28; m++; if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) { lastDay = 31; } else if(m==4 || m==6 || m==9 || m==11) { lastDay = 30; } else if(isLeapYear(y)==true) { lastDay = 29; } return lastDay; } </script>
Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template