从毫秒数字转换为日期对象Javascript [英] Convert from string with milliseconds to date object Javascript

查看:106
本文介绍了从毫秒数字转换为日期对象Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理日期时间转换时,我遇到了这个问题。我有来自postgreSQL数据库的时间戳数据,格式如下所示:



2011-04-04 19:27:39.92034 b
$ b

为了以高图形显示它,我必须将它转换为日期或时间对象。如果没有毫秒,我可以使用 Date.js 轻松转换它。



<但毫秒不能用该库处理。我也尝试过使用Date.parse,但总是得到NaN。


这个问题的解决方案?谢谢

建立在Date类中的JS应该能够处理这个,并且getTime()可以返回自1970年以来的毫秒数( UNIX时间)。不过要注意时区问题;构造函数可能会将日期/时间解释为本地,但自1970年以来,getTime()的毫秒数可能为UTC,因此难以删除的转换中烘焙。

  new Date(2011-04-04 19:27:39.92034)。getTime()
1301941659920


I got this problem when dealing with date time conversion. I have timestamp data from postgreSQL database with format like this one

"2011-04-04 19:27:39.92034"

In order to display it in highcharts, I have to convert it to date or time object. Without milliseconds, I easily convert it with Date.js

But milliseconds can't be handled with that library. I tried also with Date.parse but always got NaN.

Any solution for this problem? Thank you

解决方案

JS built in Date class should be able to handle this, and getTime() can return milliseconds since start 1970 (UNIX time). Watch out for time zone issues though; the constructor may interpret the date/time as being local, but getTime()'s milliseconds since 1970 may be in UTC, baking in a conversion that is difficult to remove.

new Date("2011-04-04 19:27:39.92034").getTime()
1301941659920

这篇关于从毫秒数字转换为日期对象Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆