JS instance last week next week code

Source: Internet
Author: User


<script>
var Currdt;
var aryday = new Array ("Day", "one", "two", "three", "four", "five", "six");

Initial page

function ini () {
Currdt = new Date ();

Showdate ();
}

Last 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;
Make sure Monday is the day
if (dw==0) {
TDDT = Adddate (currdt,-6);
}
else {
TDDT = Adddate (Currdt, (1-DW));
}

Show a week's date in a 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 highlighting
}


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, by positive and negative decisions of NUM, positive for Plus, minus for 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 by ope, + for Plus,-for minus, or not moving
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;
}

Whether it is a leap year
function Isleapyear (y) {
var isleap = false;
if (y%4==0 && y%100!=0 | | y%400==0) {
Isleap = true;
}
return isleap;
}

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 style= "Cursor:hand;font-weight:bold onclick=" Addweek ('-') "title=" Last week ">←</span>
<span id= "Span1" ></span>
<span style= "Cursor:hand;font-weight:bold onclick=" addweek (' + ') "title=" Next week ">→</span>


<table id= "TB1" border= "1" style= "font-size:10pt" >
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

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.