node.js无法解析ISOString日期?

时间:2022-01-31 17:12:01

We store every date data in ISO format using new Date().toISOString().

我们使用新的Date()。toISOString()以ISO格式存储每个日期数据。

I tried to convert this ISO formatted date into Date object in node.js but I get Invalid Date response.

我试图将此ISO格式的日期转换为node.js中的Date对象,但我得到无效日期响应。

date string is isoDate = 2014-07-09T14:00:00.000Z and I did console.log on Date.parse(isoDate); and new Date(isoDate); but each returns NaN and Invalid Date.

日期字符串是isoDate = 2014-07-09T14:00:00.000Z,我在Date.parse上执行了console.log(isoDate);和新的日期(isoDate);但每个都返回NaN和无效日期。

I checked if the date string contains any invisible wrong character but they are fine and can be converted on browser console.

我检查了日期字符串是否包含任何不可见的错误字符,但它们很好,可以在浏览器控制台上转换。

does this mean I need to convert the string manually and create Date object with parsed string?

这是否意味着我需要手动转换字符串并使用已解析的字符串创建Date对象?

Thanks for reading.

谢谢阅读。

2 个解决方案

#1


2  

Try using moment library. It has a lot of functionality to work with dates and can easily be used both on client and server side. Calling moment("2014-07-09T14:00:00.000Z").toDate() would convert your string to a Date JavaScript Object, using this library.

尝试使用时刻库​​。它具有许多处理日期的功能,可以在客户端和服务器端轻松使用。调用时刻(“2014-07-09T14:00:00.000Z”)。toDate()会使用此库将您的字符串转换为Date JavaScript对象。

#2


0  

I am posting this answer just in case somebody experience this like I did.

我发布这个答案,以防有人像我一样经历这个。

What happened to me is I thought I was sending an ISOString from the browser

发生在我身上的是我以为我是从浏览器发送ISOString的

{
  startDate: date.startDate
}

which in fact I was sending a moment instance as parameter

实际上我发送了一个瞬间实例作为参数

When I checked in the network inspector I found out that the data being sent is in ISO format - yes, but it is enclosed in double quote ""

当我检查网络检查员时,我发现发送的数据是ISO格式 - 是的,但是用双引号括起来“”

{
  startDate: "2016-12-31T16:00:00.000Z"
}

it should not be enclosed in double qoutes and should look like this

它不应该用双qoutes括起来,应该是这样的

{
  startDate: 2016-12-31T16:00:00.000Z
}

what worked for me is to parse the moment to iso string

对我有用的是将时刻解析为iso string

{
  startDate: date.startDate.toISOString()
}

#1


2  

Try using moment library. It has a lot of functionality to work with dates and can easily be used both on client and server side. Calling moment("2014-07-09T14:00:00.000Z").toDate() would convert your string to a Date JavaScript Object, using this library.

尝试使用时刻库​​。它具有许多处理日期的功能,可以在客户端和服务器端轻松使用。调用时刻(“2014-07-09T14:00:00.000Z”)。toDate()会使用此库将您的字符串转换为Date JavaScript对象。

#2


0  

I am posting this answer just in case somebody experience this like I did.

我发布这个答案,以防有人像我一样经历这个。

What happened to me is I thought I was sending an ISOString from the browser

发生在我身上的是我以为我是从浏览器发送ISOString的

{
  startDate: date.startDate
}

which in fact I was sending a moment instance as parameter

实际上我发送了一个瞬间实例作为参数

When I checked in the network inspector I found out that the data being sent is in ISO format - yes, but it is enclosed in double quote ""

当我检查网络检查员时,我发现发送的数据是ISO格式 - 是的,但是用双引号括起来“”

{
  startDate: "2016-12-31T16:00:00.000Z"
}

it should not be enclosed in double qoutes and should look like this

它不应该用双qoutes括起来,应该是这样的

{
  startDate: 2016-12-31T16:00:00.000Z
}

what worked for me is to parse the moment to iso string

对我有用的是将时刻解析为iso string

{
  startDate: date.startDate.toISOString()
}