/* 最简单的时钟 */ function getTime() { var now = new Date(); var result = {}; result.year = getYear(now); result.month = repairWithZero(getMonth(now)); result.day = repairWithZero(getDay(now)); result.week = getWeek(now); result.hour = repairWithZero(getHours(now)); result.minute = repairWithZero(getMinutes(now)); result.second = repairWithZero(getSeconds(now)); return result; } function formatTimeCh(obj) { var str = ''; var weeks = ['日', '一', '二', '三', '四', '五', '六']; str = `${obj.year}年${obj.month}月${obj.day}日星期${weeks[obj.week]} ${obj.hour}:${obj.minute}:${obj.second}`; document.getElementById('time').innerHTML = str; } function formatTimeEn(obj) { var str = ''; var hourStr; var weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; if (obj.hour > 12) { hourStr = `${obj.hour-12}:${obj.minute}:${obj.second} PM`; } else { hourStr = `${obj.hour}:${obj.minute}:${obj.second} AM`; } str = `${obj.year}-${obj.month}-${obj.day} ${weeks[obj.week]} ${hourStr}`; document.getElementById('etime').innerHTML = str; } function formatTime(obj) { formatTimeCh(obj); formatTimeEn(obj) } setInterval(function () { formatTime(getTime()); }, 1000) function repairWithZero(num) { if (num < 0) { return false; } else if (num < 10) { return '0' + num; } else { return num; } } function getYear(date) { return date.getFullYear(); } function getMonth(date) { return date.getMonth() + 1; } function getDay(date) { return date.getDate(); } function getWeek(date) { return date.getDay(); } function getHours(date) { return date.getHours(); } function getMinutes(date) { return date.getMinutes(); } function getSeconds(date) { return date.getSeconds(); }