The js implementation calendar can get the specified date week and day of the week. Example share of js gets the day of the week

  • 2020-03-30 02:23:47
  • OfStack

Because there should be interaction, choose Js to achieve, is also a pair of programming preliminary test. I'll write the display section in HTML, and the button that I clicked to trigger the event function is check();


function onCheck(){ 
var Year = document.getElementById("year").value; // Get the year of the text box  var theYear =Year * 1; // convert number type  //alert(theYear); // Value for month
var month = document.getElementById("month"); 
var index1=month.selectedIndex; var theMonth = month.options[index1].value; //Value for month
var day = document.getElementById("day"); 
var index2=day.selectedIndex; 
var theDay = day.options[index2].value;
//Input value judgment section
...
//Calling the core function
days(theYear,theMonth,theDay); 
}

The core function days is as follows:


function days(year,month,day) { 
    var days = 0;  //Change the date to the day of the year
    //Cumulative month days
    for(var i = 1; i < month; i++ ){
    switch(i){
    //Big month plus 31
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:{
    days += 31;
    break;
    }
    //So let's add 30 to the smaller month
    case 4:
    case 6:
    case 9:
    case 11:{
    days += 30;
    break;
    }
    //For February, add by year type
    case 2:{
        if(isLeapYear(year)){
        days += 29; //Leap year plus 29
        }
        else {
        days += 28;
        }
    break;
    }
    }
}
day = day * 1;
days += day;  //The sum of the days of the month plus the days of the day
var date0 = new Date(year,0,1);   //The first day of the year was the day of the week
//   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);  //Take down the whole
//   alert(nthOfWeek);
    var toDay = new Array(" Sunday "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "); 
    //Day.to getDay (); Some of the week in which 0 is Sunday by Date
    alert(" The date is the first of the year "+days+" day n"+"      Is the first "+nthOfWeek+" Weeks of "+toDay[date1.getDay()]);
}

During debugging, we encountered many unexpected errors, such as calculation errors caused by type mismatch, such as rounding of Numbers.
With the help of my teammates, he was responsible for auditing and catching bugs, while I was responsible for implementing and coding.
In the last part, in the test of input value, we assisted each other very well, analyzed different input situations, covered all kinds of possible accidents, and quickly completed the improvement of function.
Here is the code to determine whether the input value is allowed:


if (isNaN(theYear)|| theYear < 0) {
  alert(" There is an error in the input. Please re-enter ");
  return ;
}
if((theMonth == 2 && theDay > 29 && isLeapYear(theYear))||(theMonth == 2 && theDay > 28 && !isLeapYear(theYear))) {
  alert(" There is an error in the input. Please re-enter ");
  return ;
} 
if((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && theDay == 31 ) {
  alert(" There is an error in the input. Please re-enter ");
  return ;
} 


Related articles: