标准时间转常用时间
changeTime(time) {
var date = new Date(time); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = 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();
return Y + M + D + h + m + s;
},
输出:
console.log(changeTime("2021-05-14T02:57:52.000+00:00"))
// 2021-05-14 10:57:52
标准时间转英文时间
changeTime(time) {
const date = new Date(time); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
const months = new Array(
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Spt',
'Oct',
'Nov',
'Dec',
);
const week = new Array(
'Monday',
'Tuseday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
);
const day = new Array('st', 'nd', 'rd', 'th');
const getMonth = date.getMonth();
const getWeek = date.getDay();
const getDate = date.getDate();
let dd;
if (getDate % 10 < 1 || getDate % 10 > 3) {
dd = day[3];
} else {
dd = day[(getDate % 10) - 1];
if (getDate == 11 || getDate == 12) {
dd = day[3];
}
}
const date_str= months[getMonth] + ' ' + getDate + dd + ' ' + week[getWeek - 1] + ' ' + date.getFullYear();
return date_str;
}
输出:
console.log(changeTime("2021-05-14T02:57:52.000+00:00"))
//May 14th Friday 2021