日期格式与时间戳相互转换

日期格式与时间戳相互转换

1.将日期格式转化为时间戳:

var date = new Date(‘2018-06-08 18:00:00‘);    // 有三种方式获取    var time1 = date.getTime();    var time2 = date.valueOf();    var time3 = Date.parse(date);    console.log(time1);//1528452000000    console.log(time2);//1528452000000    console.log(time3);//1528452000000

苹果手机里这样使用会因为日期中间有空格导致结果为NaN,可以将日期分割然后进行转化

var str=‘2018-06-08 18:00:00‘ ;

var time=new Date(str.split(" ")).getTime();

2.将时间戳转化为日期格式:

function timestampToTime(timestamp) {        var  date = new Date(timestamp * 1000);//时间戳为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() + ‘:‘;        var m = date.getMinutes() + ‘:‘;        var s = date.getSeconds();        return Y+M+D+h+m+s;    }    timestampToTime(1528452000);    console.log(timestampToTime(1528452000));//2018-06-08 18:00:00

大佬链接

https://blog.csdn.net/weixin_42333548/article/details/80539239

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部