HTML:
<div class="checktime">
<div class="date"></div>
<div>
<span class="day"></span>
<span class="time"></span>
</div>
</div>
//引入 time.js 和 gettime.js
//[gettime.js 部份]
‘use strict’;
var week_arr = ['日', '一', '二', '三', '四', '五', '六'];
// 补零函数
function addZero(num) {
return num >= 10 ? num : '0' + num;
}
// 获取时间函数
function getTime() {
var data = new Date();
var Hours = addZero(data.getHours());
var Minute = addZero(data.getMinutes());
var Year = data.getFullYear();
var Month = addZero(data.getMonth()+1);
var Second= addZero(data.getSeconds());
// 周几
var Day = data.getDay();
// 日期天
var date = addZero(data.getDate());
return {
Hours: Hours,
Minute: Minute,
Year: Year,
Month: Month,
Second:Second,
Day: Day,
date: date,
};
}
;(
// 获取时间函数,页面直接展示
function() {
var data = new Date();
var Hours = addZero(data.getHours());
var Minute = addZero(data.getMinutes());
var Year = data.getFullYear();
var Month = addZero(data.getMonth()+1);
var Second= addZero(data.getSeconds());
// 周几
var Day = data.getDay();
// 日期天
var date = addZero(data.getDate());
window.TIME = {
Hours: Hours,
Minute: Minute,
Year: Year,
Month: Month,
Second:Second,
Day: Day,
date: date
};
})();
//[time.js 部份]
//获取到页面上时间所处于的位置,并把时间放入页面内
var date = document.getElementsByClassName('date')[0];
var day = document.getElementsByClassName('day')[0];
var time = document.getElementsByClassName('time')[0];
date.innerText = TIME.Year + '年' + TIME.Month + '月' +(TIME.date)+'日';
day.innerText = "周"+week_arr[TIME.Day];
time.innerText = (TIME.Hours) + " : " +(TIME.Minute);
var goback = document.getElementsByClassName('goback')[0];
function getTime1(){
var TIME = getTime();//调用 getTime.js
date = document.getElementsByClassName('date')[0];
day = document.getElementsByClassName('day')[0];
time = document.getElementsByClassName('time')[0];
date.innerText = TIME.Year + '年' + TIME.Month + '月' +(TIME.date)+'日';
day.innerText = "周"+week_arr[TIME.Day];
time.innerText = (TIME.Hours) + " : " +(TIME.Minute) + ":" +(TIME.Second);
var goback = document.getElementsByClassName('goback')[0];
}
setInterval(function(){
getTime1();
},1000)