SoFunction
Updated on 2025-04-06

JS implements calendar to get the specified date, week number and week number sharing examples (js get the week number)

It should be necessary to have interaction, and Js is chosen to implement it, which can be regarded as a first test of pairing programming. I wrote the display part in html, and the button clicked trigger event function is check();

Copy the codeThe code is as follows:

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

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

The core function days are as follows:

Copy the codeThe code is as follows:

function days(year,month,day) {
var days = 0; //Indicate what day the change date is the same as that of the year
//Cumulative number of months
    for(var i = 1; i < month; i++ ){
    switch(i){
//The situation of the big month is 31 more
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:{
    days += 31;
    break;
    }
//Xiaoyue's situation adds 30
    case 4:
    case 6:
    case 9:
    case 11:{
    days += 30;
    break;
    }
//The situation in February will be added according to the year type
    case 2:{
        if(isLeapYear(year)){
days += 29; //Add 29 in leap year
        }
        else {
        days += 28;
        }
    break;
    }
    }
}
day = day * 1;
days += day;  //The sum of the number of days plus the number of days

var date0 = new Date(year,0,1);   //The first day of that year was the week
//   alert(());
var date1 = new Date(year,month-1,day); //Format the date value, 0-11 represents January-December;
//   alert((days + ()+6)/7);
var nthOfWeek = ((days + ()+6)/7); //Sum downward
//   alert(nthOfWeek);
var toDay = new Array("Sunday","Monday","Tuesday","Weekday","Thursday","Friday","Saturday");
//();Return to a certain Sunday in a week according to Date
alert("This date is the "+days+"day\n"+"     It is the "+nthOfWeek+" weekly"+toDay[()]);
}

During the debugging process, many unexpected errors were encountered, such as calculation errors caused by type mismatch, such as rounding of numbers;
With the assistance of his teammates, he is responsible for reviewing and assisting in catching bugs, and I am responsible for implementing and coding;
In the last part, in the test of input values, we assisted each other well, analyzed different input situations, covered various possible accidents, and quickly completed the improvement of the functions;
The following is the code to determine whether the input value is allowed:

Copy the codeThe code is as follows:

if (isNaN(theYear)|| theYear < 0) {
alert("If the input is incorrect, please re-enter");
  return ;
}

if((theMonth == 2 && theDay > 29 && isLeapYear(theYear))||(theMonth == 2 && theDay > 28 && !isLeapYear(theYear))) {
alert("If the input is incorrect, please re-enter");
  return ;
}

if((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && theDay == 31 ) {
alert("If the input is incorrect, please re-enter");
  return ;
}