Home > Web Front-end > JS Tutorial > body text

js implements calendar to get the week number and day of the week for a specified date. Example sharing (js gets the day of the week)_Basic knowledge

WBOY
Release: 2016-05-16 16:55:39
Original
1078 people have browsed it

Because there is interaction, I chose Js to implement it. It can be regarded as the first trial of pair programming. I will write the display part in html, and the event function triggered by the clicked button is check();

Copy code The code is as follows:

function onCheck(){
var Year = document .getElementById("year").value; //Get the "year" of the text box var theYear =Year * 1; //Convert to number type //alert(theYear); //Get the month value
var month = document.getElementById("month");
var index1=month.selectedIndex; var theMonth = month.options[index1].value; //Get the monthly value
var day = document.getElementById("day") ;
var index2=day.selectedIndex;
var theDay = day.options[index2].value;

//Input value judgment part
...
//Call core function
days(theYear,theMonth,theDay);
}

The core function days are as follows:

Copy code The code is as follows:

function days(year,month,day) {
var days = 0; //Indicates the day of the year when the date is changed
//Add the number of days in the month
for(var i = 1; i < month; i ){
switch(i) {
//Add 31 to the big moon
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:{
days = 31;
break;
}
//In Xiaoyue’s case, add 30
case 4:
case 6:
case 9:
case 11:{
days = 30;
break;
}
//For February, add
according to the year type case 2:{
if( isLeapYear(year)){
days = 29; //Add 29 for leap years
}
days = 28;
}
break;
}
}
}
day = day * 1;
days = day; //The sum of the number of days in months plus the number of days in days

var date0 = new Date(year,0,1); //What day of the week is the first day of the year?

// alert(date0.getDay());
var date1 = new Date(year ,month-1,day); //Format the date value, 0-11 represents January-December;
// alert((days date0.getDay() 6)/7);
var nthOfWeek = Math.floor((days date0.getDay() 6)/7); // Round down
// alert(nthOfWeek);
var toDay = new Array("Sunday","week "1", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
//day.getDay(); Returns a day of the week based on Date, where 0 is Sunday
alert("This date is the " days " of the year " " It is the " nthOfWeek " week " toDay[date1.getDay()]);
}

We encountered many unexpected errors during the debugging process, such as calculation errors caused by type mismatch, such as number rounding problems;

With the assistance of teammates, he was responsible for reviewing and assisting in catching bugs, and I was responsible for Implementation and coding;
In the last step, during the testing of input values, we assisted each other very well, analyzed different input situations, covered various possible accidents, and quickly completed the improvement of the function;
The following is the code to determine whether the input value is allowed:

Copy code The code is as follows:
if (isNaN(theYear)|| theYear < 0 ) {
alert("Incorrect input, please re-enter");
return ;
}

if((theMonth == 2 && theDay > 29 && isLeapYear(theYear))||(theMonth == 2 && theDay > 28 && !isLeapYear(theYear))) {

alert("Enter Yes Wrong, please re-enter");
return ;
}

if((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && theDay == 31 ) {

alert("Incorrect input, please re-enter" );
return ;
}

Related labels:
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