Javascript simple year-month-date linkage implementation code _ time-Date

Source: Internet
Author: User
JavaScript code is implemented by simple year-on-year interaction. For more information, see. HTML

The Code is as follows:





Source code:

The Code is as follows:


Function vYearMonth (yearObjId, monthObjId ){
Var selYear = document. getElementById (yearObjId );
Var selMonth = document. getElementById (monthObjId );
Var myDate = new Date (); // current Date
Var myYear = myDate. getFullYear (); // current year
Var myMonth = myDate. getMonth () + 1; // current month
Var yearMin =-2; // The year range value, which is also the difference value of the current year comparison.
Var yearMax = 10; // The year range value, which is also the difference value of the current year comparison.

// * In *******************************
SelYear. options. add (new Option ("",""));
For (var I = yearMin; I <yearMax; I ++ ){
Var opt = new Option (myYear + I, myYear + I );
SelYear. options. add (opt );
}
// Here 1-yearMin indicates that the current year is selected, and 1 is used because "" is inserted at the beginning ""
SelYear. options. selectedIndex = 1-yearMin;
// End year *******************************

// In month *******************************
SelMonth. options. add (new Option ("",""));
For (var I = 0; I <12; I ++ ){
Var opt = new Option (I + 1, I + 1 );
SelMonth. options. add (opt );
}
// Select the current month
SelMonth. options. selectedIndex = myMonth;
// End month *******************************


SelYear. onchange = function (){
If (this. value = ""){
SelMonth. selectedIndex = 0;
}
Else {
If (selMonth. value = ""){
SelMonth. selectedIndex = myMonth;
}
}
};
}


Call:

The Code is as follows:


VYearMonth ('seltjyear', 'seltjmonth ');


Complete DEMO code:

<Select name = "SelTjYear"> </SELECT> <select name = "SelTjMonth"> </SELECT> script function vYearMonth (yearObjId, monthObjId) {var selYear = document. getElementById (yearObjId); var selMonth = document. getElementById (monthObjId); var myDate = new Date (); // the current Date var myYear = myDate. getFullYear (); // var myMonth = myDate of the current year. getMonth () + 1; // var yearMin =-2 for the current month; // The year range value, which is also the difference var yearMax = 10 for the current year comparison; // The year range value, the difference value of the current year comparison:/Begin year ****************************** * selYear. options. add (new Option ("", ""); for (var I = yearMin; I <yearMax; I ++) {var opt = new Option (myYear + I, myYear + I); selYear. options. add (opt);} // here 1-yearMin indicates the current year, and 1 is used because "" selYear "is inserted at the beginning. options. selectedIndex = 1-yearMin; // End year ******************************* // Begin month * * ****************************** selMonth. options. add (new Option ("", ""); for (var I = 0; I <12; I ++) {var opt = new Option (I + 1, I + 1); selMonth. options. add (opt);} // select selMonth of the current month. options. selectedIndex = myMonth; // End month ******************************** selYear. onchange = function () {if (this. value = "") {selMonth. selectedIndex = 0;} else {if (selMonth. value = "") {selMonth. selectedIndex = myMonth ;}};} vYearMonth ('seltjyear', 'seltjmonth'); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

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.