时间 new Date()
获取时间,可以理解为是系统默认的函数。 从小括号里面获取系统时间日期,相当于在调用系统默认的函数。 年 getFullYear() 注意,有Full。 月 getMonth() 注意,系统是从0而不是人1开始。 日 getDate() 注意,是Date不是day。 周 getDay() 注意,这个才是day。 时 getHours() 分 getMinutes() 秒 getSeconds()
var mydate= new Date(); console.log(mydate) //Fri Dec 09 2016 13:58:48 GMT+0800 (中国标准时间) //2016年9号 13点48分 //使用的是 GMT 格式的时候 //下面获取 (get) 年份 var iYear=mydate.getFullYear(); console.log(iYear) //2016 年 var iMonth=mydate.getMonth(); console.log(iMonth) //11, 月份,系统使用的月份是从0开始的,范围是0到11。 console.log(iMonth+1) //12, 月份,用户使用的月份 var iDay=mydate.getDate(); //天是Date不是day。 console.log(iDay) //9, var iWeek=mydate.getDay(); //星期才是day console.log(iWeek) //5 var iHours=mydate.getHours(); //小时 console.log(iHours) //14 var iMin=mydate.getMinutes(); //分钟 console.log(iMin) //13 var iSec=mydate.getSeconds(); //秒 console.log(iSec) //33 |
每秒更新的时钟
setInterval( function (){ var mydate= new Date(); var mydate2=(mydate.getFullYear()+ "年 " + (mydate.getMonth()+1)+ "月 " + mydate.getDate()+ "日 " + " 星期" +mydate.getDay()+ " " + mydate.getHours()+ "时 " + mydate.getMinutes()+ "分 " + mydate.getSeconds()+ "秒 " ); document.getElementsByTagName( "body" )[0].innerHTML=mydate2; },1000) |
点击显示每秒更新的时间,补0
document.write( '<input type="button" value="显示时间" ><input>' ); var ipt=document.getElementsByTagName( "input" ); var i_h; var i_m; var i_s; ipt[0].onclick= function (){ function time_box(){ //时间获取函数 var i_time= new Date(); if (i_time.getHours()<10){ //如果小于10,补上0 i_h= "0" +i_time.getHours(); } else { i_h=i_time.getHours(); } if (i_time.getMinutes()<10){ i_m= "0" +i_time.getMinutes(); } else { i_m=i_time.getMinutes(); } if (i_time.getSeconds()<10){ i_s= "0" +i_time.getSeconds(); } else { i_s=i_time.getSeconds(); } ipt[1].value=i_h + " : " + i_m + " : " + i_s; //改变输入框里的值 } setInterval(time_box,1000); //定时器,每秒执行 } |
也可以自己写个补0函数
function orz(a){ if (a<10){ return "0" +a } else { return a } }
|
© 著作权归作者所有
举报
发表评论
0/200