html语言 读取系统时间 显示的时间时NaN时什么原因啊

html语言 读取系统时间 显示的时间时NaN时什么原因啊,第1张

浏览器兼容问题,日期对象在每个浏览器中实现的api多多少少都有些差别。

第一种改法:

// 改动1

time(h1,'2018,06,30')  // 在一起的时间

time(h1, 2018, 5, 30)

// 改动2:

function time(obj,futimg)

function time(obj, _year, _month, _day)

// 改动3:

var futruetime = new Date(futimg).getTime() // 未来时间转换为时间戳

var futruetime = new Date(_year, _month, _day).getTime()

第二种改法:

time(h1,'2018,06,30') // 在一起的时间

time(h1,'2018-06-30')

只是兼容问题:火狐:getFullYear   其他浏览器:getYear

if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){

这里用 getFullYear

}else{

这里就用 getYear

}

写两套吧兄弟

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>系统时间</title>

<script language="javascript" type="text/javascript">

<!--

//获得当前时间,刻度为一千分一秒

var initializationTime=(new Date()).getTime()

function showLeftTime()

{

var now=new Date()

var year=now.getYear()

var month=now.getMonth()

var day=now.getDate()

var weekday=now.get

var hours=now.getHours()

var minutes=now.getMinutes()

var seconds=now.getSeconds()

document.all.show.innerHTML=""+year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds+""

//一秒刷新一次显示时间

var timeID=setTimeout(showLeftTime,1000)

}

function showtime() {

var date = new Date()

this.year = date.getFullYear()

this.month = date.getMonth() + 1

this.date = date.getDate()

this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()]

this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()

this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()

this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()

var currentTime = "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " +"("+ this.day+")"

document.all.show.innerHTML=currentTime

var timei=setTimeout(showtime,1000)}

//-->

</script>

</head>

<body onload="showtime()">

<label id="show">显示时间的位置</label>

</body>

</html>

手懒,我找了一份别人的代码贴给你看吧!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7505877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存