| 1234567891011121314151617181920212223242526272829303132 |
- /**
- * 公用时间显示功能
- * 格式:yyyy年MM月dd日星期X HH:mm:ss
- */
- export function initTimeDisplay(elementId) {
- // 时间更新函数
- function updateNativeTime() {
- const now = new Date();
- const year = now.getFullYear();
- const month = String(now.getMonth() + 1).padStart(2, '0');
- const day = String(now.getDate()).padStart(2, '0');
- const hours = String(now.getHours()).padStart(2, '0');
- const minutes = String(now.getMinutes()).padStart(2, '0');
- const seconds = String(now.getSeconds()).padStart(2, '0');
- const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
- const weekday = weekdays[now.getDay()];
- // 移除月份和日期的前导零
- const monthNum = now.getMonth() + 1;
- const dayNum = now.getDate();
- const timeString = `${year}年${monthNum}月${dayNum}日 ${weekday} ${hours}:${minutes}:${seconds}`;
-
- const timeElement = document.getElementById(elementId);
- if (timeElement) {
- timeElement.textContent = timeString;
- }
- }
- // 立即执行一次
- updateNativeTime();
- // 每秒更新
- setInterval(updateNativeTime, 1000);
- }
|