Recently encountered a query page in a project where one of the criteria is to display the selected date, month, and number of weeks according to the selected year, such as a requirement. Search on the internet for a while, there are similar but not exactly the same, have to do their own hands. The idea is to get the selected number 1th the day of the week, and then work out the Monday date of the week.
On this basis, the end of the 1th week is the day plus 6, the 2nd week begins on this day plus 7, the end date for this day plus 13, and so on until the beginning of the 5th week and the end date are calculated.
The specific code is as follows:
The method has two parameters, date is a given datetime parameter, and days is the number that needs to be added and reduced on that date.
For example, add (2010-10-01,-3) returns a date that is 3 days back from 2010-10-01 2010-09-28 function adddate (date,days) {var d=new date (date);
D.setdate (D.getdate () +days);
var m=d.getmonth () +1;
var tmpdate = d.getfullyear () + "/" + M + "/" + d.getdate ();
var resultdate = new Date (tmpdate);
return resultdate;
The Format function Changedateformat (Datein) {var year = 0) converted to YYYY-MM-DD for the selected year
var month = 0;
var day = 0;
var currentdate = "";
Year = Datein.getyear ();
month = Datein.getmonth () + 1;
Day = Datein.getdate ();
currentdate = year + "-";
if (month >) {currentdate = currentdate + month + "-";
}else{currentdate = currentdate + "0" + month + "-";
} if (Day >) {currentdate = currentdate + day;
}else{currentdate = currentdate + "0" + day;
return currentdate;
///According to the week of the week calculated the start date and End Day function weektodate (Weeknumber,strweekday,datein) {//Output start day var outputdate_1 = "";
End of output var outputdate_2 = "";
var tmp_date= ""; If you choose Number 1th, this day happens to be s.Unday, the week Monday date is the date of this day-6 if (strweekday = = ' 0 ') {outputdate_1 = Adddate (datein,-6);
//If the selected year 1th is Monday, the second parameter is set to 0, and so on.
if (Strweekday = = ' 1 ') {outputdate_1 = Adddate (datein,0);
} if (Strweekday = = ' 2 ') {outputdate_1 = Adddate (datein,-1);
} if (Strweekday = = ' 3 ') {outputdate_1 = Adddate (datein,-2);
} if (Strweekday = = ' 4 ') {outputdate_1 = Adddate (datein,-3);
} if (Strweekday = = ' 5 ') {outputdate_1 = Adddate (datein,-4);
} if (Strweekday = = ' 6 ') {outputdate_1 = Adddate (datein,-5);
} if (WeekNumber = = ' 1 ') {outputdate_2 = Datein;
} if (WeekNumber = = ' 2 ') {outputdate_2 = Adddate (outputdate_1,+7);
} if (WeekNumber = = ' 3 ') {outputdate_2 = Adddate (outputdate_1,+14);
} if (WeekNumber = = ' 4 ') {outputdate_2 = Adddate (outputdate_1,+21);
} if (WeekNumber = = ' 5 ') {outputdate_2 = Adddate (outputdate_1,+28); if (WeekNumber = = ' 1 ') {//The beginning day of the 1th week the calculation basis is outputdate_1, so the document.getElementById ("Spndate") is judged separately. Value =
Changedateformat (outputdate_1); Temp_date = Adddate (outPUTDATE_1,+6);
document.getElementById ("Finishtime"). Value =changedateformat (temp_date); }else{///2nd week after the beginning of the day end of the day are based on outputdate_2 as the basis document.getElementById ("Spndate"). Value = Changedateformat (outputdate_2)
;
Temp_date = Adddate (outputdate_2,+6);
document.getElementById ("Finishtime"). Value =changedateformat (temp_date);
} function Getfromtodateofselectedweek () {var year = document.getElementById ("Sltyear");
var optionyear=year.getelementsbytagname ("option");
var stryear = "";
for (Var i=0;i<optionyear.length;++i) {if (optionyear[i].selected) {stryear = Optionyear[i].text;
} var month = document.getElementById ("Sltmonth");
var option = month.getelementsbytagname ("option");
var strmonth = "";
for (Var i=0;i<option.length;++i) {if (option[i].selected) {strmonth = Option[i].text;
}///According to the selected year, combine into yyyy/mm/01 date string var sltdate_tmp_0 = stryear+ "/" + Strmonth + "/" + "1";
Converts a date string to a date-type var sltdate_tmp_1 = new Date (SLTDATE_TMP_0); Get the number of weeks of the month 1th, var weekday = Sltdate_tmp_1.getday ();
Gets the number of weeks for the page selection var weeknumber = document.getElementById ("WeekNumber");
var optionweek=weeknumber.getelementsbytagname ("option");
var strweek = "";
var tmp_date= "";
var outputdate_1 = "";
var outputdate_2 = "";
for (Var i=0;i<optionweek.length;++i) {if (optionweek[i].selected) {strweek = Optionweek[i].text; }//Select the 1th postmenstrual calculates this day for the year 1th Monday to the Sunday various possibilities, returns the week Monday to the Sunday date//The following judgment and so on if (Strweek = = ' 1 ') {if (weekday = = ' 0 ') {we
Ektodate (Strweek, ' 0 ', sltdate_tmp_1);
} if (weekday = = ' 1 ') {weektodate (Strweek, ' 1 ', sltdate_tmp_1);
} if (weekday = = ' 2 ') {weektodate (Strweek, ' 2 ', sltdate_tmp_1);
} if (weekday = = ' 3 ') {weektodate (Strweek, ' 3 ', sltdate_tmp_1);
} if (weekday = = ' 4 ') {weektodate (Strweek, ' 4 ', sltdate_tmp_1);
} if (weekday = = ' 5 ') {weektodate (Strweek, ' 5 ', sltdate_tmp_1);
} if (weekday = = ' 6 ') {weektodate (Strweek, ' 6 ', sltdate_tmp_1);
} if (Strweek = ' 2 ') {if (weekday = = ' 0 ') {weektodate (Strweek, ' 0 ', sltdate_tmp_1); } if (weekday = = '1 ') {weektodate (Strweek, ' 1 ', sltdate_tmp_1);
} if (weekday = = ' 2 ') {weektodate (Strweek, ' 2 ', sltdate_tmp_1);
} if (weekday = = ' 3 ') {weektodate (Strweek, ' 3 ', sltdate_tmp_1);
} if (weekday = = ' 4 ') {weektodate (Strweek, ' 4 ', sltdate_tmp_1);
} if (weekday = = ' 5 ') {weektodate (Strweek, ' 5 ', sltdate_tmp_1);
} if (weekday = = ' 6 ') {weektodate (Strweek, ' 6 ', sltdate_tmp_1);
} if (Strweek = ' 3 ') {if (weekday = = ' 0 ') {weektodate (Strweek, ' 0 ', sltdate_tmp_1);
} if (weekday = = ' 1 ') {weektodate (Strweek, ' 1 ', sltdate_tmp_1);
} if (weekday = = ' 2 ') {weektodate (Strweek, ' 2 ', sltdate_tmp_1);
} if (weekday = = ' 3 ') {weektodate (Strweek, ' 3 ', sltdate_tmp_1);
} if (weekday = = ' 4 ') {weektodate (Strweek, ' 4 ', sltdate_tmp_1);
} if (weekday = = ' 5 ') {weektodate (Strweek, ' 5 ', sltdate_tmp_1);
} if (weekday = = ' 6 ') {weektodate (Strweek, ' 6 ', sltdate_tmp_1);
} if (Strweek = ' 4 ') {if (weekday = = ' 0 ') {weektodate (Strweek, ' 0 ', sltdate_tmp_1);
} if (weekday = = ' 1 ') {weektodate (Strweek, ' 1 ', sltdate_tmp_1); } if (weekDay = = ' 2 ') {weektodate (Strweek, ' 2 ', sltdate_tmp_1);
} if (weekday = = ' 3 ') {weektodate (Strweek, ' 3 ', sltdate_tmp_1);
} if (weekday = = ' 4 ') {weektodate (Strweek, ' 4 ', sltdate_tmp_1);
} if (weekday = = ' 5 ') {weektodate (Strweek, ' 5 ', sltdate_tmp_1);
} if (weekday = = ' 6 ') {weektodate (Strweek, ' 6 ', sltdate_tmp_1);
} if (Strweek = ' 5 ') {if (weekday = = ' 0 ') {weektodate (Strweek, ' 0 ', sltdate_tmp_1);
} if (weekday = = ' 1 ') {weektodate (Strweek, ' 1 ', sltdate_tmp_1);
} if (weekday = = ' 2 ') {weektodate (Strweek, ' 2 ', sltdate_tmp_1);
} if (weekday = = ' 3 ') {weektodate (Strweek, ' 3 ', sltdate_tmp_1);
} if (weekday = = ' 4 ') {weektodate (Strweek, ' 4 ', sltdate_tmp_1);
} if (weekday = = ' 5 ') {weektodate (Strweek, ' 5 ', sltdate_tmp_1);
} if (weekday = = ' 6 ') {weektodate (Strweek, ' 6 ', sltdate_tmp_1); }
}
}
After testing for a bug, when February 1 is Monday and the selected year is excepting, the 5th week of February is selected, and the 5th week's start date is March instead of February. For example, for the 5th week of February 2010, the program will display from 2010-03-1 to 2010-03-07.
There are two solutions, one is when the user chooses the 5th postmenstrual the month dropdown box to force the setting to March. The second is to force the week dropdown box to the 4th week, and show 2010-02-22 to 2010 02-28.
After testing for a bug, when February 1 is Monday and the selected year is excepting, the 5th week of February is selected, and the 5th week's start date is March instead of February.
For example, for the 5th week of February 2010, the program will display from 2010-03-1 to 2010-03-07.
There are two solutions, one is when the user chooses the 5th postmenstrual the month dropdown box to force the setting to March. The second is to force the week dropdown box to the 4th week, and show 2010-02-22 to 2010 02-28.
After testing for a bug, when February 1 is Monday and the selected year is excepting, the 5th week of February is selected, and the 5th week's start date is March instead of February.
For example, for the 5th week of February 2010, the program will display from 2010-03-1 to 2010-03-07.
There are two solutions, one is when the user chooses the 5th postmenstrual the month dropdown box to force the setting to March. The second is to force the week dropdown box to the 4th week, and show 2010-02-22 to 2010 02-28.
The above is a small set to introduce the implementation of JavaScript based on the number of weeks to display the beginning of the day and the end of the implementation code, I hope to help!