JavaScript:字符串日期时间转本地日期时间

huangapple go评论67阅读模式
英文:

Javascript: string date time to local date time

问题

I have a Vue application, I am trying to convert a long UTC datetime string to a local time Javascript object, this is my string:

> Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)

I have tried (unsuccessfully):

Date.parse(string);

and

new Date(string);

Which is leaving me with something like this:

var dt = myDate.split(/-|\s/)
dat = new Date(dt.slice(0,3).reverse().join('/')+' '+dt[3]);

But this does not seem efficient?

I need to do this over a 500 object JSON, so want to ensure I am doing it as efficient as possible.

英文:

I have a Vue application, I am trying to convert a long UTC datetime string to a local time Javascript object, this is my string:

> Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)

JavaScript:字符串日期时间转本地日期时间

I have tried (unsuccessfully):

Date.parse(string);

and

new Date(string);

Which is leaving me with something like this:

var dt  = myDate.split(/\-|\s/)
dat = new Date(dt.slice(0,3).reverse().join('/')+' '+dt[3]);

But this does not seem efficient?

I need to do this over a 500 object JSON, so want to ensure I am doing it as efficient as possible.

答案1

得分: 1

If you can possibly avoid storing the date in that format, you will save a lot of headache.

如果可能的话,尽量避免以这种格式存储日期,这样你将减少很多麻烦。

Although the string is in UTC, it is in a particular language and locale. It just happens to be English and UTC which is handy for you and me to read by eye, but there is no guarantee that the user's browser will be in this language/locale.

尽管字符串处于UTC时区,它实际上是特定的语言和区域设置。它恰好是英语和UTC,对你和我来说很方便,但不能保证用户的浏览器也处于这种语言/区域设置。

If at all possible, store the date/time in ISO format, which is

如果可能的话,请以ISO格式存储日期/时间,这有以下好处:

  • easy for code to read and write

  • compact

  • easy to sort

  • 便于代码读写

  • 紧凑

  • 易于排序

Example if ISO-8601:

例如,ISO-8601格式:

2023-04-10T21:57:02Z

You can tweak it to a particular language and timezone for display purposes, but I suggest always storing it in ISO.

你可以根据需要调整它以适应特定的语言和时区,但我建议始终以ISO格式存储。

An example of why never to store anything other than ISO format in your data, is the very problem you are facing.

一个例子说明为什么永远不要在数据中存储除ISO格式之外的任何内容,就是你现在面临的问题。

Presumably you only need to fix it once, to extract the data? If so, make sure you are using a standard browser (e.g. up to date Chrome), have set the language to English, and try out the example shown by @Dimava

假设你只需要修复一次以提取数据?如果是这样,确保你使用标准浏览器(例如最新版本的Chrome),将语言设置为英语,然后尝试@Dimava示例中显示的方法。

new Date('Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)')

I get this, which is correct, but unhelpful:

我得到了这个结果,是正确的,但没有什么帮助:

Tue Apr 11 2023 00:19:45 GMT+0100 (British Summer Time)

You can then convert all of them into a sensible format:

然后,你可以将它们都转换为一个合理的格式:

new Date('Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)').toISOString()

'2023-04-10T23:19:45.000Z'

Are you sure the fromClientDateTime variables are actually strings?

你确定fromClientDateTime变量实际上是字符串吗?

I suspect they may be Javascript DateTime objects. If so, you can convert them to ISO format as follows:

我怀疑它们可能是JavaScript的DateTime对象。如果是这样,你可以按照以下方式将它们转换为ISO格式:

fromClientDateTime.toISOString()

The reason I suspect that is when I console.log a string property in Chrome, I get quotation marks. The way to get no quotation marks (as you have in your image), is to have the property be a DateTime object, not a string.

我之所以怀疑是因为当我在Chrome中使用console.log记录一个字符串属性时,我会得到引号。要消除引号(正如你在图像中看到的那样),就需要将属性设置为DateTime对象,而不是字符串。

英文:

If you can possibly avoid storing the date in that format, you will save a lot of headache

Although the string is in UTC, it is in a particular language and locale. It just happens to be English and UTC which is handy for you and me to read by eye, but there is no guarantee that the user's browser will be in this language/locale.

If at all possible, store the date/time in ISO format, which is

  • easy for code to read and write
  • compact
  • easy to sort

Example if ISO-8601:

2023-04-10T21:57:02Z

You can tweak it to a particular language and timezone for display purposes, but I suggest always storing it in ISO.

An example of why never to store anything other than ISO format in your data, is the very problem you are facing.

Presumably you only need to fix it once, to extract the data? If so, make sure you are using a standard browser (e.g. up to date Chrome), have set the language to English, and try out the example shown by @Dimava

new Date('Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)')

I get this, which is correct, but unhelpful:

Tue Apr 11 2023 00:19:45 GMT+0100 (British Summer Time)

You can then convert all of them into a sensible format:

new Date('Mon Apr 10 2023 23:19:45 GMT+0000 (Coordinated Universal Time)').toISOString()


'2023-04-10T23:19:45.000Z'

Are you sure the fromClientDateTime variables are actually strings?

I suspect they may be Javascript DateTime objects. If so, you can convert them to ISO format as follows:

fromClientDateTime.toISOString()

The reason I suspect that is when I console.log a string property in Chrome, I get quotation marks. The way to get no quotation marks (as you have in your image), is to have the property be a DateTime object, not a string.

const a = {x: new Date(), y: "Example string"}

a
{x: Mon Apr 10 2023 23:05:53 GMT+0100 (British Summer Time), y: 'Example string'}

huangapple
  • 本文由 发表于 2023年4月11日 05:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定