js时间戳转化及时间的加减

整理一下时间的转换,以及加减

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());
},

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值