方法
function FormatTimeDetail (timestamp) {
if (!isNaN(timestamp)) {
const TimestampToTime = (timestamp) => {
const date = new Date(timestamp); // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
const Y = date.getFullYear();
const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
const D = date.getDate();
const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return Y + '年' + M + '月' + D + '日' + ' ' + h + ':' + m + ':' + s;
};
const now = new Date(timestamp); // 日期
const nowDayOfWeek = now.getDay(); // 周的第几天
const nowDayOfHours = now.getHours(); // 第几小时
const weekFormat = {
1: '星期一',
2: '星期二',
3: '星期三',
4: '星期四',
5: '星期五',
6: '星期六',
0: '星期日'
};
const date = TimestampToTime(timestamp);
const week = weekFormat[nowDayOfWeek];
let timeProgress = '';
if (nowDayOfHours >= 1 && nowDayOfHours <= 5) {
timeProgress = '北京时间凌晨';
} else if (nowDayOfHours > 5 && nowDayOfHours <= 6) {
timeProgress = '北京时间清晨';
} else if (nowDayOfHours > 6 && nowDayOfHours <= 11) {
timeProgress = '北京时间早上';
} else if (nowDayOfHours > 11 && nowDayOfHours <= 13) {
timeProgress = '北京时间中午';
} else if (nowDayOfHours > 13 && nowDayOfHours <= 17) {
timeProgress = '北京时间下午';
} else if (nowDayOfHours > 17 && nowDayOfHours <= 18) {
timeProgress = '北京时间傍晚';
} else if (nowDayOfHours > 18 && nowDayOfHours <= 24) {
timeProgress = '北京时间晚上';
} else {
timeProgress = '北京时间午夜';
}
const cdata = {
'date': date,
'week': week,
'timeProgress': timeProgress
};
return cdata;
} else {
return '';
}
};
调用及打印
eg: