简体   繁体   English

在Javascript中以毫秒精度将日期转换为Unix

[英]Converting date to unix with milliseconds precision in Javascript

I am currently trying to convert a few dates in Javascript however these dates are defined with milliseconds and that data cannot be lost when converted. 我目前正在尝试用Javascript转换一些日期,但是这些日期用毫秒定义,转换后不会丢失数据。 I have tried doing the following 我尝试了以下操作

var dateString = '2009-07-15 11:00:00.675';
dateString = dateString.split(' ').join('T');
var date = new Date(dateString);
date = date.getTime() / 1000;

But date returns 但是日期返回

date= 1247655600.675

I have read a few topics in stack overflow but the only "solution" I saw was the following: 我已经阅读了堆栈溢出中的一些主题,但是我看到的唯一“解决方案”如下:

parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))

But this does not consider milliseconds either. 但这也不考虑毫秒。 What should I do to correctly convert a date to unix timestamp with milliseconds precision? 我应该怎么做才能将日期正确转换为具有毫秒精度的Unix时间戳?

Thank you. 谢谢。

try this, no need to do /1000 试试这个,不需要做/ 1000

var dateString = '2009-07-15 11:00:00.675';
dateString = dateString.split(' ').join('T');
var date = new Date(dateString);
date = date.getTime();

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

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