js实现本地时间同步功能
时间:2022-08-05 11:43:43|栏目:JavaScript代码|点击: 次
本文实例为大家分享了js实现本地时间同步的具体代码,供大家参考,具体内容如下
HTML代码
<html> <head> <title>时间</title> <meta charset="utf-8"> </head> <body onload="getTime()"> <span id="nowDateTimeSpan" ></span> </body> </html>
js代码
//时间 function getTime(){ var ndate = new Date(); var nyear = ndate.getFullYear(); var nmonth = ndate.getMonth()+1; var nday = ndate.getDate(); var nhour = ndate.getHours(); var nminute = ndate.getMinutes(); var nsecond = ndate.getSeconds(); // 如果分钟或小时的值小于10,则在其值前加0,比如如果时间是下午3点20分9秒的话,则显示15:20:09 nmonth=checkTime(nmonth); nday=checkTime(nday); nminute=checkTime(nminute); nsecond=checkTime(nsecond); document.getElementById('nowDateTimeSpan').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond; document.getElementById('nowDateTimeSpan1').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond; document.getElementById('nowDateTimeSpan2').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond; document.getElementById('nowDateTimeSpan3').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond; document.getElementById('nowDateTimeSpan4').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond; //setTimeout('getTime()',1000);//每一秒中重新加载startTime()方法 function checkTime(i){ if (i<10){ i="0" + i; } return i; } }
上一篇:js常用代码段收集
栏 目:JavaScript代码
下一篇:JavaScript表单即时验证 验证不成功不能提交
本文标题:js实现本地时间同步功能
本文地址:http://www.codeinn.net/misctech/209896.html