/**
* 时间戳格式化
* @param {Number} datetime 时间戳(秒)
* @param {String} formateType 时间戳所需处理的方式 normal(月日时分),info(年月日时分秒),rule(年月日时分秒,date(年月日)
* @param {String} joinType 日期间隔符号
* @return {String} 日期字符串
*/
formatTime(datetime, formateType, joinType) {
const date = new Date(Number(datetime) * 1000),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds(),
formatNumber = function formatNumber(n) {
n = n.toString();
return n[1] ? n : '0' + n;
},
timeType = {
'normal': [
[month, day],
[hour, minute]
],
'info': [
[year, month, day],
[hour, minute, second]
],
'rule': [
[year, month, day],
[hour, minute]
]
};
if (formateType == 'date') {
return [year, month, day].map(formatNumber).join(joinType);
} else {
return timeType[formateType][0].map(formatNumber).join(joinType) + ' ' + timeType[formateType][1].map(formatNumber)
.join(':');
}
}
时间戳格式化
最新推荐文章于 2024-06-27 19:52:56 发布