function getTime(time = new Date()) {
const timeStr = ["零", "一","二","三","四","五","六", "七", "八", "九", "十"];
const newTime = new Date(time);
function zero(time) {
return time > 9 ? time : '0' + time;
}
function getTimeStr(time) {
const a = parseInt(time / 10);
const b = time % 10;
if (time <= 10) {
return timeStr[time];
} else if (time < 20) {
return '十' + timeStr[b];
} else if (time % 10 === 0) {
return timeStr[a] + '十';
} else {
return timeStr[a] + '十' + timeStr[b];
}
}
function yearStr(year) {
const a = parseInt(year / 1000);
const b = parseInt(year % 1000 / 100);
const c = parseInt(year % 100 / 10);
const d = year % 10;
return timeStr[a] + timeStr[b] + timeStr[c] + timeStr[d];
}
const year = newTime.getFullYear();
const month = newTime.getMonth() + 1;
const day = newTime.getDate();
const hours = newTime.getHours();
const minutes = newTime.getMinutes();
const seconds = newTime.getSeconds();
const date = newTime.getDay();
return `${yearStr(year)}年${getTimeStr(month)}月${getTimeStr(day)}日星期${getTimeStr(date)}${hours > 12 ? '下午' : '上午'}${getTimeStr(hours > 12 ? hours - 12 : hours)}点${getTimeStr(minutes)}分${getTimeStr(seconds)}秒`;
}
const nowTime = getTime(1636721972000);
console.log(nowTime);