一、
JavaScript获取当前日期时间同时显示星期几,具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< 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 > |
二、
在网页上及时动态显示当前的日期时间并显示星期的做法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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); //动态调取时间
}
解释下: var datelocalnow=new Date(); 建立一个时间类 var datelocalyear=datelocalnow.getFullYear(); 获取年份 var datelocalmonth=(datelocalmonth="0"+(datelocalnow.getMonth()+1)).substr(datelocalmonth.length-2,2); 获取月份 var datelocalday=(datelocalday="0"+datelocalnow.getDate()).substr(datelocalday.length-2,2); 获取日期 var datelocalweekday=datelocalweek[datelocalnow.getDay()]; 获取星期 datelocalnow.getHours() 获取小时 datelocalnow.getMinutes() 获取分钟 datelocalnow.getSeconds() 获取秒
四、 |