JS控制显示时间

JavaScript010

JS控制显示时间,第1张

一般的处理方式是在前台通过JS控制,JS控制显示时间的代码如下,各种不同的显示方式:

[javascript]

function Clock() {

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()

this.toString = function() {

return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day

}//现在是<span id="clock">现在是:2013年3月6日 13:54:17 星期三</span>

[javascript]

<span></span>

this.toSimpleDate = function() {

return this.year + "-" + this.month + "-" + this.date

}//2013-03-06

this.toDetailDate = function() {

return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second

}//2013-03-06 13:45:43

this.display = function(ele) {

var clock = new Clock()

ele.innerHTML = clock.toString()//显示方式调用

window.setTimeout(function() {clock.display(ele)}, 1000)

}

}

你的代码有些小问题,至于什么问题,对照着我的看看吧!

<html>

<head>

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

<body >

   <script language="javascript">

       var cur = new Date()

       var y = cur.getFullYear()

       var m = cur.getMonth()+1

       var d = cur.getDate()

       document.write("当前时间显示:"+y+" 年"+m+"月"+d+"日")

   </script>

</body>

</html>

 效果就是这样了!很简单的!

希望能帮助到你,望采纳!

JavaScript获取当前日期时间同时显示星期几,具体代码如下:

<html>

<head>

<meta

http-equiv="Content-Type"

content="text/html

charset=utf-8"

/>

<script

type="text/javascript"

src="/jquery/1.7.0/jquery.min.js"></script>

<script

type="text/javascript">

function

currentTime(){

var

d=new

Date(),str=''

str+=d.getFullYear()+'年'

str+=d.getMonth()

+

1+'月'

str+=d.getDate()+'日'

str+=d.getHours()+'时'

str+=d.getMinutes()+'分'

str+=

d.getSeconds()+'秒'

return

str

}

setInterval(function(){$('#time').html(currentTime)},1000)

</script>

</head>

<body>

<div

id="time"></div>

</body>

</html>

在网页上及时动态显示当前的日期时间并显示星期的做法:

function

showTime(){

var

show_day=new

Array('星期一','星期二','星期三','星期四','星期五','星期六','星期日')

var

time=new

Date()

var

year=time.getYear()

var

month=time.getMonth()

var

date=time.getDate()

var

day=time.getDay()

var

hour=time.getHours()

var

minutes=time.getMinutes()

var

second=time.getSeconds()

month<10?month='0'+month:month

month=month+1

hour<10?hour='0'+hour:hour

minutes<10?minutes='0'+minutes:minutes

second<10?second='0'+second:second

var

now_time='当前时间:'+year+'年'+month+'月'+date+'日'+'

'+show_day[day-1]+'

'+hour+':'+minutes+':'+second

document.getElementById('showtime').innerHTML=now_time

setTimeout("showTime()",1000)

}

关于这方面的内容网上很多,有js获取当前日期时间同时显示星期,js获取当前时间和一星期钱的时间等内容,都很具有参考价值,希望大家多阅读类似文章,将类似方法熟练掌握。