Js calendar can be used to obtain the week number of a specified date and the day of the week (js gets the day of the week)

Source: Internet
Author: User
Tags date1

For interaction, we should select Js for implementation, which is also the first test of Pair programming. I will write the display part in html, and click the button to trigger the event function as check ();

Copy codeThe Code is as follows:
Function onCheck (){
Var Year = document. getElementById ("year "). value; // obtain the "Year" var theYear = Year * 1 of the text box; // convert it to the number type // alert (theYear); // obtain the monthly value
Var month = document. getElementById ("month ");
Var index1 = month. selectedIndex; var theMonth = month. options [index1]. value; // obtain the monthly value.
Var day = document. getElementById ("day ");
Var index2 = day. selectedIndex;
Var theDay = day. options [index2]. value;

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

The core function days are as follows:

Copy codeThe Code is as follows:
Function days (year, month, day ){
Var days = 0; // indicates the day when the date is changed to the current year.
// Accumulate the number of days of the month
For (var I = 1; I <month; I ++ ){
Switch (I ){
// Increase the monthly subscription by 31
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12 :{
Days + = 31;
Break;
}
// Add 30 for a small month
Case 4:
Case 6:
Case 9:
Case 11 :{
Days + = 30;
Break;
}
// Add the year February based on the year type
Case 2 :{
If (isLeapYear (year )){
Days + = 29; // leap year plus 29
}
Else {
Days + = 28;
}
Break;
}
}
}
Day = day * 1;
Days + = day; // the number of days of the month and the number of days of the last day

Var date0 = new Date (year,); // the first day of the year is 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); // round down
// Alert (nthOfWeek );
Var toDay = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
// Day. getDay (); return one of the week's values based on Date. 0 is Sunday.
Alert ("this date is the day of the year" + days + "day \ n" + "is the day" + nthOfWeek + "Week" + toDay [date1.getDay ()]);
}

Many unexpected errors were encountered during debugging, such as computing errors caused by Type mismatch, such as number rounding;
With the assistance of his teammates, he reviews and helps catch bugs. I am responsible for implementation and coding;
In the last step, during the testing of input values, we helped each other to analyze different input conditions, covering various possible accidents and quickly completed functional improvements;
The following code determines whether the input value is allowed:

Copy codeThe Code is as follows:
If (isNaN (theYear) | theYear <0 ){
Alert ("incorrect input, please enter again ");
Return;
}

If (theMonth = 2 & theDay> 29 & isLeapYear (theYear) | (theMonth = 2 & theDay> 28 &&! IsLeapYear (theYear ))){
Alert ("incorrect input, please enter again ");
Return;
}

If (theMonth = 4 | theMonth = 6 | theMonth = 9 | theMonth = 11) & theDay = 31 ){
Alert ("incorrect input, please enter again ");
Return;
}

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.