第一个是类的方法实现,第二个是函数方式。推荐函数方式:
class UtcToLocal {
static format(str) {
return str > 9 ? str : '0' + str;
}
setUp(utcTime) {
this.date = new Date(utcTime);
this.year = this.date.getFullYear();
this.mon = UtcToLocal.format(this.date.getMonth() + 1);
this.day = UtcToLocal.format(this.date.getDate());
this.hour = UtcToLocal.format(this.date.getHours());
this.min = UtcToLocal.format(this.date.getMinutes());
this.second = UtcToLocal.format(this.date.getSeconds());
this.splitTime();
}
splitTime() {
const localTime = `${this.year}-${this.mon}-${this.day} ${this.hour}:${this.min}:${this.second}`;
console.log(localTime)
return localTime;
}
}
export default UtcToLocal;
export default function utcToLocal(utcTime) {
if (!utcTime) {
return;
}
const date = new Date(utcTime);
const year = date.getFullYear();
const mon = format(date.getMonth() + 1);
const day = format(date.getDate());
const hour = format(date.getHours());
const min = format(date.getMinutes());
const second = format(date.getSeconds());
const localTime = `${year}-${mon}-${day} ${hour}:${min}:${second}`;
return localTime;
}
function format(str) {
return str > 9 ? str : '0' + str;
}
第三个是使用moment.js
moment.utc(commonOne.valid_receive_end_time).local().format('YYYY-MM-DD hh:mm:ss')