整理一下时间的转换,以及加减
1 获取时间
var myDate = new Date();//获取的是格林威治时间
//Wed Jan 19 2022 10:52:03 GMT+0800 (中国标准时间)
2 如果需要时间转成时间戳
getTime()这个函数即可
console.log(myDate.getTime())
//1642574024786
时间戳转成时间(格林威治时间 )
console.log(new Date(sjc))
//Wed Jan 19 2022 14:35:59 GMT+0800 (中国标准时间)
3 需要将格林威治时间 ,转成标准时间
console.log(this.gettime (myDate));
2022-01-19 11:19:50
gettime(data) {
let value = data.getFullYear() + '-' +
this.checkTime(data.getMonth() + 1) + '-' +
this.checkTime(data.getDate()) + ' ' +
this.checkTime(data.getHours()) + ':' +
this.checkTime(data.getMinutes()) + ':' +
this.checkTime(data.getSeconds());
return value
},
//转换标准时间
checkTime(i) {
if (i < 10) {
i = "0" + i
}
return i;
},
4 如果需要增加/减少时间(标准时间)
console.log(this.gettime (this.converTimeV2(myDate)));
converTimeV2(timeString) {
const time = new Date(timeString);
return new Date(time.getFullYear(), time.getMonth(), time.getDate(), time.getHours()+1, time
.getMinutes(), time.getSeconds());
},