原生date格式转换为标准时间和时间戳 & 时间戳转换为原生date

var date = new Date( 2014-04-23 18:55:49:123 );

// 有三种方式获取

var time1 = date.getTime();

var time2 = date.valueOf();

var time3 = Date.parse(date);

console.log(time1);//1398250549123

console.log(time2);//1398250549123

console.log(time3);//1398250549000

时间戳转换为原生date

使用npm安装moment

import moment from “moment”;

moment.unix(时间戳).format(“YYYY-MM-DD HH:mm:ss”)

// 如果上面代码得到的时间错误,可以写为

moment.unix(时间戳/1000).format(“YYYY-MM-DD HH:mm:ss”)

// 由于时间戳有10位(默认精度为秒)和13位(默认精度为毫秒)之分

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
陳柏君的头像 - 鹿快
评论 抢沙发

请登录后发表评论

    暂无评论内容