在同一个HTML中 多个地方获取当前系统时间怎么做
在同一个HTML中多个地方获取当前系统时间怎么做如:<div>当前时间是:xxxx/xx/xx</div><div>当前时间是:xxxx/xx/xx</div><div>...
在同一个HTML中 多个地方获取当前系统时间怎么做
如:
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div> 展开
如:
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div>
<div>当前时间是:xxxx/xx/xx</div> 展开
展开全部
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <!doctype html> < html > < head > < meta charset = "utf-8" > < title ></ title > < body > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > </ body > < script > var divs = document.getElementsByClassName("nowDate"); for(var i=0;i< divs.length ;i++){ divs[i].innerText = "当前时间是:"+ getNowFormatDate(); } //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "/"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; } </ script > </ html > |
更多追问追答
追问
完全正确,如果我要显示时间怎么做比如 2019/01/07 17:10
追答
改一下就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!doctype html> < html > < head > < meta charset = "utf-8" > < title ></ title > < body > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > < div class = "nowDate" >当前时间是:xxxx/xx/xx</ div > </ body > < script > var divs = document.getElementsByClassName("nowDate"); for(var i=0;i< divs.length ;i++){ divs[i].innerText = "当前时间是:"+ getNowFormatDate(); } //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "/"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var h = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = year + seperator1 + month + seperator1 + strDate+" "+h+":"+m+":"+s; return currentdate; } </ script > </ html > |
本回答被提问者采纳
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询