JS operation Date to achieve the last week and next week function _ Time and date

Source: Internet
Author: User
Tags getdate time and date
Today, a working student turned to me for help:
1. Page load, a place to display the current date, its left and right each one arrow, respectively, said last week, the next week
The table below shows each date of the week, from Monday to Sunday
2. Point left and RIGHT arrow, that place displays the previous week or the next week, and the following table shows each date of the previous week or next week

JS itself does not provide date addition and subtraction operation capability, so I wrote several methods to operate the date, mainly to achieve the date of the addition and reduction
Author: Chenzhou Extension Vocational Nimenmin

<script> var Currdt; var aryday = new Array ("Day", "one", "two", "three", "four", "five", "six"); Initial page function ini () {Currdt = new Date (); Showdate (); //previous week or next week 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 ();//Show date var dw = Currdt.getday (); var Tddt; Determine Monday is the day if (dw==0) {TDDT = Adddate (currdt,-6); else {TDDT = Adddate (Currdt, (1-DW)); ///Show the date of the week in the table 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 Highlight} DW = Tddt.getday (); objtb.rows[0].cells[i].innerhtml = Tddt.getmonth () +1 + "Month" + tddt.getdate () + "Day Week" + ARYDAY[DW]; TDDT = Adddate (tddt,1); Next day}//increase or decrease for several days, determined by Num's positive and negative, positive for plus, minus 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; }//Increase or decrease day, determined by Ope, + for Plus,-for minus, otherwise fixed 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; }//Is leap year function isleapyear (y) {var isleap = false; if (y%4==0 && y%100!=0 | | | y%400==0) {isleap = true; return isleap; The last day of the month 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> <body onload= "ini ()" > <!--Load-time initial page--> <span onclick= "Addweek ('-')" title= "Last week" >← </span>   <span id= "Span1" ></span>   <span onclick= "Addweek" (' + ') "title=" Next week ">→</span> <table id=" tb1 "border=" 1 "> <tr> <td></td> <td></td> & lt;td></td> <td></td> <td></td> <td></td> <td></td> </ Tr> </table>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.