HTML中如何實現時間在頁面中實時更新

2021-10-08 05:13:28 字數 3529 閱讀 1532

<

!doctype html>

"utf-8"

>

time<

/title>

var date =

newdate()

;var year = date.

getfullyear()

;var month = date.

getmonth()

+1; month=month<10?

"0"+month:month;

var day = date.

getdate()

; day=day<10?

"0"+day:day;

var week = date.

getday()

;switch

(week)

case2:

case3:

case4:

case5:

case6:

case0:

}var hour = date.

gethours()

; hour= hour<10?

"0"+ hour: hour;

var minute = date.

getminutes()

; minute=minute<10?

"0"+minute:minute;

var second = date.

getseconds()

; second=second<10?

"0"+second:second;

console.

log(year+

"."+month+

"."+day+

" "+week+

" "+hour+

":"+minute+

":"+second)

;<

/script>

<

/head>

<

/body>

<

/html>

getfullyear()、getmonth()、getdate()、getday():

從 date 物件中分別返回四位數字表示的年份、月份 (0 ~ 11)、乙個月中的某一天 (1 ~ 31)、一周中的某一天 (0 ~ 6)

gethours()、getminutes()、getseconds():

返回 date 物件的小時 (0 ~ 23)、分鐘 (0 ~ 59)、秒數 (0 ~ 59)。

這種方法無法在頁面中顯示,只能通過控制台來檢視。

2.在頁面中顯示:

我們需要通過document.getbyid(「id」).innerhtml來實現,**為:

innerhtml:為頁面新增元素

"time"

>

<

/span>

var date =

newdate()

;var year = date.

getfullyear()

;var month = date.

getmonth()

+1; month=month<10?

"0"+month:month;

var day = date.

getdate()

; day=day<10?

"0"+day:day;

var week = date.

getday()

;switch

(week)

case2:

case3:

case4:

case5:

case6:

case0:

}var hour = date.

gethours()

; hour= hour<10?

"0"+ hour: hour;

var minute = date.

getminutes()

; minute=minute<10?

"0"+minute:minute;

var second = date.

getseconds()

; second=second<10?

"0"+second:second;

var result = year+

"."+month+

"."+day+

" "+week+

" "+hour+

":"+minute+

":"+second ;

document.

getelementbyid

("time"

).innerhtml = result;

<

/script>

<

/body>

3.實現實時更新

如果想實現時間實時更新,則需要借助setinterval(「方法」,時間)來實現。

"time"

>

<

/span>

function

show()

case2:

case3:

case4:

case5:

case6:

case0:

}var hour = date.

gethours()

; hour= hour<10?

"0"+ hour: hour;

var minute = date.

getminutes()

; minute=minute<10?

"0"+minute:minute;

var second = date.

getseconds()

; second=second<10?

"0"+second:second;

var result = year+

"."+month+

"."+day+

" "+week+

" "+hour+

":"+minute+

":"+second ;

document.

getelementbyid

("time"

).innerhtml = result;

}show()

;setinterval

("show()"

,1000);

<

/script>

<

/body>

可利用charat方法對**進行優化:

week=

"日一二三四五六"

.charat

(week)

; week=

"星期"

+week;

<

/script>

在Oracle中實現時間相加處理

在oracle中實現時間相加處理 名稱 add times 功能 返回d1與newtime相加以後的結果,實現時間的相加 說明 對於newtime中的日期不予考慮 日期 版本 1.0 create or replace function add times d1 in date,newtime in...

在Oracle中實現時間相加處理

在oracle中實現時間相加處理 名稱 add times 功能 返回d1與newtime相加以後的結果,實現時間的相加 說明 對於newtime中的日期不予考慮 日期 版本 1.0 create or replace function add times d1 in date,newtime in...

HTML頁面中顯示時間

charset utf 8 title type text css span span hover style head time span varshow function show setinterval show 1000 script body html js內建物件date的使用 js中有...