Home > Web Front-end > JS Tutorial > javascript drop-down box displays current date_time and date

javascript drop-down box displays current date_time and date

WBOY
Release: 2016-05-16 19:07:47
Original
1520 people have browsed it

Places that need to be added:

Copy code The code is as follows:





Test code:

Copy code The code is as follows:






* 您消费的时间












* 您消费的时间











<script> <br>var today=new Date()//Define a time object<br>var yy=today.getYear() <br> var mm=today.getMonth() 1 <br>var dd=today.getDate() <br>var h=today.getHours()//define hours <br>var m=today.getMinutes()//define minutes <br>document.getElementById("years").options(yy-2007).selected=1 <br>document.getElementById("months").options(mm-1).selected=1 <br>document.getElementById ("days").options(dd-1).selected=1 <br>document.getElementById("hours").options(h).selected=1 <br>document.getElementById("mins").options( m).selected=1 <br></script>



Second method: No need to write the year in advance , better scalability

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

The third method: cannot But the programming idea is OK. If it is used formally, it is recommended to use the second method The code is as follows:




New Document


< script>
function setDay(obj){
obj = obj.form;
var years=parseInt(obj.years.options[obj.years.selectedIndex].value);
var months= parseInt(obj.months.options[obj.months.selectedIndex].value);
if(obj.years.selectedIndex==0 || obj.months.selectedIndex==0)return;
var lastday = monthday(years,months);
var itemnum = obj.days.length;
if (lastday - 1 < obj.days.selectedIndex)
{
obj.days.selectedIndex = lastday - 1;
}
obj.days.length = lastday;
for(cnt = itemnum 1;cnt <= lastday;cnt )
{
obj.days.options[cnt - 1].text = cnt;
}
}
function monthday(years,months)
{
var lastday = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (((years % 4 == 0) && (years % 100 != 0)) || (years % 400 == 0))
{
lastday[1] = 29;
}
return lastday[months - 1];
}
function forto(ff,to)
{
document. write('');
for(var ii=ff; ii<=to; ii )
document.write('');
}
function a()
{
alert(document.all("years").value "Years " document.all("months").value "month" document.all("days").value "day") ;
}


Years
month
< ;SELECT name="days">Day




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template