node.js 如何将字符串转换为时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10967937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:51:18  来源:igfitidea点击:

How to convert string to timestamp?

node.js

提问by Bdfy

How to convert string to timestamp ? For example, I have string:

如何将字符串转换为时间戳?例如,我有字符串:

2012-06-10T10:00:00+04:00

回答by Behnam Esmaili

Use Parse method of Date object as described here.

使用此处描述的 Date 对象的 Parse 方法。

回答by Ankit

Try momentpackage for all date parsing it's awesome package.

尝试所有日期解析的moment包,这是很棒的包。

回答by Mustafa

If you are asking this for converting your date to database-specific formats, you can use a very nice library moment.jsfor various conversions

如果您要求将日期转换为特定于数据库的格式,您可以使用一个非常好的库moment.js进行各种转换

回答by sbp

I tried following in AWS node js lambda function (so mostly like js) and worked fine.

我尝试在 AWS 节点 js lambda 函数(所以大多像 js)中遵循并且工作正常。

  var arrivalTime='2012-06-10T10:00:00+04:00';
  var d = new Date(arrivalTime);
  console.log('This is -> d ' + d.toString());

For node js lambda the only issue is it always returns UTC so conversion is needed but for js (i.e. on client side) it would return in local time I'd think.

对于节点 js lambda,唯一的问题是它总是返回 UTC,因此需要进行转换,但对于 js(即在客户端),它会在我认为的本地时间返回。