码迷,mamicode.com
首页 > Web开发 > 详细

js时间与毫秒互相转换

时间:2018-07-09 15:16:53      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:str   bsp   日期格式   regexp   直接   ons   replace   日期格   方法   

1)日期转换为毫秒

如果格式是:yyyy/mm/dd hh:mm:ss可以直接转换。var oldTime = (new Date("2018/07/09 14:13:11")).getTime(); //得到毫秒数  

如果日期格式是:yyyy-mm-dd hh:mm:ss需要转化格式

    var startDate =‘2018-07-09 14:13:11‘;
    startDate= startDate.replace(new RegExp("-","gm"),"/");
    var startDateM = (new Date(startDate)).getTime(); //得到毫秒数

 

日期转化为毫秒的另一种方法:

var str = ‘2018-07-09 14:13:11‘;

var arr = str.split(/[- : \/]/);
var startDate = Date.parse(new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]));
console.log(startDate)

 

2)毫秒数转化为时间

var endDate = (new Date("2018/072/09 14:13:11")).getTime(); //得到毫秒数  
var newDate = new Date(endDate ); //得到普通的时间了 

 

js时间与毫秒互相转换

标签:str   bsp   日期格式   regexp   直接   ons   replace   日期格   方法   

原文地址:https://www.cnblogs.com/qing619/p/9283618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!