本文目錄一覽:
- 1、js顯示當前時間
- 2、關於JavaScript顯示時間的一段簡單代碼!
- 3、js顯示當前日期時間和星期幾
- 4、js獲得當前日期和時間的代碼是什麼?
- 5、用js在網頁上顯示當前日期和時間,並顯示是星期幾
- 6、JS控制顯示時間
js顯示當前時間
你的代碼有些小問題,至於什麼問題,對照着我的看看吧!
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
body
script language=”JavaScript” type=”text/JavaScript”
!–
var isnMonth = new Array(“1月”,”2月”,”3月”,”4月”,”5月”,”6月”,”7月”,”8月”,”9月”,”10月”,”11月”,”12月”);
var isnDay = new Array(“星期日”,”星期一”,”星期二”,”星期三”,”星期四”,”星期五”,”星期六”,”星期日”);
today = new Date () ;
if(document.all){
document.write(“font color=\”#27C28D\”\n”);
document.write(isnMonth[today.getMonth()]+today.getDate()+”日 “+isnDay[today.getDay()]);
document.write(“/font”);}
//–
/script
/body
/html
暈,我可不是看分的面上來的
只是覺得這段代碼很簡潔,想介紹給樓主而已
js顯示當前日期時間和星期幾
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();
month10?month=’0’+month:month;
month=month+1;
hour10?hour=’0’+hour:hour;
minutes10?minutes=’0’+minutes:minutes;
second10?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獲取當前時間和一星期錢的時間等內容,都很具有參考價值,希望大家多閱讀類似文章,將類似方法熟練掌握。
js獲得當前日期和時間的代碼是什麼?
var
myDate
=
new
Date();
myDate.toLocaleDateString();可以獲取當前日期
myDate.toLocaleTimeString();
可以獲取當前時間
擴展:
myDate.getYear();
//獲取當前年份(2位)
myDate.getFullYear();
//獲取完整的年份(4位,1970-????)
myDate.getMonth();
//獲取當前月份(0-11,0代表1月)
myDate.getDate();
//獲取當前日(1-31)
myDate.getDay();
//獲取當前星期X(0-6,0代表星期天)
myDate.getTime();
//獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours();
//獲取當前小時數(0-23)
myDate.getMinutes();
//獲取當前分鐘數(0-59)
myDate.getSeconds();
//獲取當前秒數(0-59)
myDate.getMilliseconds();
//獲取當前毫秒數(0-999)
myDate.toLocaleString(
);
//獲取日期與時間
用js在網頁上顯示當前日期和時間,並顯示是星期幾
1、要獲得當前時間,我們可以使用Date方法,new一個Date對象。
2、然後調用這個對象的toLocaleTimeString方法,來獲取具體的時間
3、最後使用alert方法來彈出結果,來驗證一下是否正確
4、運行頁面,點擊按鈕,可以看到彈出一個確認框里,裏面顯示的時間就是當前時間了。
JS控制顯示時間
一般的處理方式是在前台通過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);
};
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/303102.html