JS Implementation Calendar can get the specified date weeks and days of the week sample sharing (JS get the day of the Week) _ Basic knowledge

Source: Internet
Author: User
Tags date1

Should be interactive, choose JS to achieve, is also a pair of programming for the preliminary. I'll have the display part written in HTML, click on the button to trigger the event function is check ();

Copy Code code as follows:

function Oncheck () {
var year = document.getElementById ("year"). Value; Gets the text box "year" var theyear =year * 1; Convert to Number type//alert (theyear); Get month value
var month = document.getElementById ("month");
var Index1=month.selectedindex; var themonth = Month.options[index1].value; Get month value
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 are as follows:

Copy Code code as follows:

Function days (year,month,day) {
var days = 0; Change date to day of year
Cumulative month Days
for (var i = 1; i < month; i++) {
switch (i) {
Semi-rotary, plus 31.
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:{
days = 31;
Break
}
The circumstances of the Moon plus 30.
Case 4:
Case 6:
Case 9:
Case 11:{
days = 30;
Break
}
February situation, according to the type of year to add
Case 2:{
if (Isleapyear (year)) {
days = 29; Leap year plus 29
}
else {
days = 28;
}
Break
}
}
}
Day = day * 1;
Days = day; Number of days of the month plus days

var date0 = new Date (year,0,1); The first day of the year is the week
Alert (Date0.getday ());
var date1 = new Date (year,month-1,day); Format Date values, 0-11 represents January-December;
Alert (days + date0.getday () +6)/7);
var nthofweek = Math.floor (days + date0.getday () +6)/7); Rounding down
alert (Nthofweek);
var today = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
Day.getday (): 0 of the one weeks returned according to date is Sunday
Alert ("The date is the" +days+ "Day of the Year \ n" + "is the" +nthofweek+ "Week" +today[date1.getday ()]);
}

Many unexpected errors were encountered during debugging, such as the calculation errors caused by type mismatches, such as rounding of numbers;
With the help of his teammates, he is responsible for reviewing and assisting in catching bugs, and I am responsible for implementing and coding;
In the last link, the input value of the test, we are very good mutual assistance, analysis of different input, covering a variety of possible accidents, quickly completed the function of perfection;
The following is the code that determines whether the input value is allowed:

Copy Code code as follows:

if (isNaN (theyear) | | | theyear < 0) {
Alert ("Input is incorrect, please re-enter");
return;
}

if ((Themonth = 2 && theday > && isleapyear (theyear)) | | (Themonth = 2 && theday > &&!isleapyear (theyear)) {
Alert ("Input is incorrect, please re-enter");
return;
}

if (Themonth = = 4 | | themonth = 6 | | | themonth = 9 | | themonth = && Theday = 31) {
Alert ("Input is incorrect, please re-enter");
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.