function timestampToTime(timestamp, type) {
var date = new Date(timestamp);
var Y = date.getFullYear();
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
switch (type) {
case 'y': // 年
return Y;
case 'y-m': // 年-月
return Y + '/' + M;
case 'h-m': // 时:分
return h + ':' + m;
case 'm-s': // 分:秒
return m + ':' + s;
case 'h-m-s': // 时:分:秒
return h + ':' + m + ':' + s;
case 'y-m-d-h-m': // 年-月-日 时:分
return Y + '/' + M + '/' + D + '' + h + ':' + ':' + m;
case 'y-m-d-h-m-s': // 年-月-日 时:分:秒
return Y + '/' + M + '/' + D + '' + h + ':' + m + ':' + s;
case 'm-d-h-m-s': // 月-日 时:分:秒
return M + '月' + D + '日' + h + ':' + m + ':' + s;
default: // 默认 年-月-日
return Y + '-' + M + '-' + D;
}
}
console.log(timestampToTime(1564624526889, "m-d-h-m-s"))
console.log(timestampToTime(1634745600 * 1000))
console.log(timestampToTime(1634745600 * 1000))
console.log(timestampToTime(1646977318 * 1000, "y-m-d-h-m-s"))
时间戳转化
最新推荐文章于 2024-06-13 14:23:28 发布