简体   繁体   English

如何使用JavaScript将字符串时间戳转换为日期

[英]How to convert string timestamp to date using javascript

I created some funtion but how to convert string timestamp to date format for the given variabe. 我创建了一些功能,但是如何将字符串时间戳转换为给定variabe的日期格式。

var data="/Date(1424853425207)/";
data=data.parse(data);
consoe.log(data);

But enabe show the date for the above variable 但是enabe显示上述变量的日期

You can fetch the timestamp using regex and then cast to a number so it can be used to create a new instance of Date : 您可以使用regex获取时间戳,然后将其强制转换为数字,以便可以使用它创建Date的新实例:

var data = "/Date(1424853425207)/";
var timestamp = +data.replace(/\/Date\((.*?)\)\//g, '$1');
var date = new Date(timestamp);
console.log(date); // Wed Feb 25 2015 09:37:05 GMT+0100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM