Use the JQuery method to implement date-time selection in JavaScript files

Source: Internet
Author: User
Tags getdate

JavaScript Document

$ (document). Ready (function (e) {

display the current date in a text box
var date = new Date ();
var Nian = Date.getfullyear ();
var yue = date.getmonth () +1;
var Tian = date.getdate ();

$ ("#rq"). Val (nian+ "-" +yue+ "-" +tian ");

Add a Click event to a text box
$ ("#rq"). Click (function () {
$ (' #myModal '). Modal (' show ');
Load Year
Loadnian ();
Load Month
Loadyue ();
Load Days
Loadtian ();
})

When you select a change, reload the day
$ ("#nian"). Change (function () {
Loadtian ();
})
when the change is selected in the current month, reload the day
$ ("#yue"). Change (function () {
Loadtian ();
})
Add Click events to the OK button
$ ("#qd"). Click (function () {
var Nian = $ ("#nian"). Val ();
var yue = $ ("#yue"). Val ();
var Tian = $ ("#tian"). Val ();

$ ("#rq"). Val (nian+ "-" +yue+ "-" +tian ");
$ (' #myModal '). Modal (' hide ');
})

});

method of loading year
function Loadnian ()
{
var date = new Date ();
var Nian = Date.getfullyear ();

var str = "";
For (var i=nian-5;i<nian+6;i++)
{
if (I==nian)
{
str = str + "<option selected= ' selected ' value= '" +i+ "' >" +i+ "</option>";
}
Else
{
str = str + "<option value=" "+i+" > "+i+" </option> ";
}
}
$ ("#nian"). html (str);
}

Load Month
function Loadyue ()
{
var date = new Date ();
var yue = date.getmonth () +1;

var str = "";
For (var i=1;i<13;i++)
{
if (I==yue)
{
str = str+ "<option selected= ' selected ' value= '" +i+ "' >" +i+ "</option>";
}
Else
{
str = str+ "<option value= '" +i+ ">" +i+ "</option>";
}
}
$ ("#yue"). html (str);
}

Load Days
function Loadtian ()
{
var date = new Date ();
var Tian = date.getdate ();
var yue = $ ("#yue"). Val ();
var Nian = $ ("#nian"). Val ();

var zs = +;
if (yue==4 | | yue==6 | | yue==9 | | yue==11)
{
ZS = +;
}
else if (yue==2)
{
if ((nian%4==0 && nian%100!=0) | | nian%400==0)
{
ZS =;
}
Else
{
ZS =;
}
}

var str = "";
For (var i=1;i<zs+1;i++)
{
if (I==tian)
{
str = str + "<option selected= ' selected ' value= '" +i+ "' >" +i+ "</option>";
}
Else
{
str = str + "<option value=" "+i+" > "+i+" </option> ";
}
}

$ ("#tian"). html (str);

}

Use the JQuery method to implement date-time selection in JavaScript files

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.