<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <script src="../public/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script> <title>Document</title> </head> <body> <select id="nian" onchange="changeTian()"> </select>年 <select id="yue" onchange="changeTian()"> </select>月 <select id="tian"> </select>日 <div id="shijian"> </div> </body> </html> <script type="text/javascript"> nian(); yue(); tian(); showTime(); function showTime(){ var nowTime = new Date(); $("#shijian").html(nowTime.toLocaleString());//Sun Jun 24 2018 19:41:21 GMT+0800 转成2018/6/24 下午7:42:09 setTimeout("showTime()",1000);//用于在指定的毫秒数后调用函数或计算表达式。 } function nian(){ var nowtime = new Date();//获取系统当前时间 var nian = nowtime.getFullYear();//获取当前年份 var str = ""; for(var i=1900;i<=2020;i++){ if(i==nian){ str +="<option selected>"+i+"</option>" }else{ str +="<option>"+i+"</option>"; } }$("#nian").html(str); } function yue(){ var myDate = new Date(); var month=myDate.getMonth()+1;//获取当前月 var str =""; for(var i =1;i<=12;i++){ if(i==month){ str +="<option selected>"+i+"</option>" //选中当前月 }else{ str +="<option>"+i+"</option>"; } } $("#yue").html(str); } function tian(){ var nian = $("#nian").val(); var yue = $("#yue").val(); var ts =30; if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12){ ts = 31; }else if(yue==2){ if((nian%4==0&&nian%100 !=0)||nian%400==0){ ts = 29; }else{ ts=28; } } var str =""; for(var i=1;i<=ts;i++){ str+="<option>"+i+"</option>"; } $("#tian").html(str); } function changeTian(){ tian(); } </script>