Posted in Javascript onJune 17, 2019
本文实例讲述了javascript系统时间设置操作。分享给大家供大家参考,具体如下:
<script> //myTime typeof //object window.onload = function() { var obody = document.body; //设置一个定时器,每过一秒钟,自动加一秒 setInterval(fnTime, 1000) //如果不执行这个函数,第一次打开时,会过一秒钟才出来 fnTime(); function fnTime() { var myTime = new Date(); var iYear = myTime.getFullYear(); var iMonth = myTime.getMonth() + 1; var iDate = myTime.getDate(); var iWeek = myTime.getDay(); var iHours = myTime.getHours(); var iMinutes = myTime.getMinutes(); var iSeconds = myTime.getSeconds(); var str = ""; if(iWeek === 0) iWeek = "星期日" if(iWeek === 1) iWeek = "星期一" if(iWeek === 2) iWeek = "星期二" if(iWeek === 3) iWeek = "星期三" if(iWeek === 4) iWeek = "星期四" if(iWeek === 5) iWeek = "星期五" if(iWeek === 6) iWeek = "星期六" str = iYear + "年" + iMonth + "月" + iDate + "日 " + iWeek + " " + toTwo(iHours) + "时" + toTwo(iMinutes) + "分" + toTwo(iSeconds) + "秒" obody.innerHTML = str; } console.log(toTwo(05))//05 console.log(toTwo(15))//15 //这个函数主要是让:时分秒到59秒时,再走一秒就变成了0,而不是00,所以要转换一下 function toTwo(n) { /* if(n < 10) { return "0" + n; } else { return "" + n; } */ //我们可以用三目 return n < 10 ? "0" + n : "" + n } } </script>
运行结果:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码运行效果。
javascript系统时间设置操作示例
- Author -
longzhoufeng声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@