Javascript从已知语言环境的toLocaleDateString解析日期 [英] Javascript parse date from toLocaleDateString for known locale

查看:83
本文介绍了Javascript从已知语言环境的toLocaleDateString解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想根据用户的区域设置格式化日期。如果用户修改了日期,我希望能够将其解析回javascript Date对象。

In my application I want to format dates according to the user's locale. If the user modifies the date, I want to be able to parse it back into a javascript Date object.

我想知道是否可以使用javascript从本地格式化的字符串中解析日期?

I wonder if it's possible to parse a date from a locally formatted string using javascript?

请考虑以下代码段:

new Date(2016, 0, 1).toLocaleDateString('ar');
>>"١‏/١‏/٢٠١٦"

如果我想要将该字符串解析回有效日期我将如何处理?

If I want to parse that string back to a valid date how would I go about that?

以下情况不起作用:

new Date(new Date(2016, 0, 1).toLocaleDateString('ar'));
>>Invalid Date

我还尝试使用 momentjs 但无法在那里找到合适的方式。

I also experimented with using momentjs but was unable to find a working way there.

//change global locale
moment.locale('ar-sa');

moment().format('L');
>>"١٥/٠٦/٢٠١٦"

moment(new moment().format('L'))._d;
>>Invalid Date

还是有一些其他/更好的思考方式像这样的问题?

Or is there some other/better way to think around issues like this?

推荐答案

由于JS Date的工作方式,确实没有一个好方法可以做到这一点Date对象。它没有一致的解析行为。

Because of the way JS Date works, there really isn't a good way to do this with just the Date object. It doesn't have consistent parsing behavior.

Moment会为你做这个,而你实际上非常接近。我不确定你为什么要刻意调用新关键字。这不是必要的。要使用Moment获取阿拉伯语日期:

Moment will do this for you, and you're actually quite close. I'm not sure why you're calling the new keyword on moment. That isn't necessary. To get an Arabic date with Moment:

moment.locale('ar-sa'); 
moment().format('L')
"١٥/٠٦/٢٠١٦"

要将该日期解析为片刻:

To parse that date back into a moment:

moment("١٥/٠٦/٢٠١٦", 'L').format()
"٢٠١٦-٠٦-١٥T٠٠:٠٠:٠٠-٠٥:٠٠"

或者如果您想要英文字符与API进行交互:

Or if you want English characters for interacting with an API:

moment("١٥/٠٦/٢٠١٦", 'L').locale('en').format()
"2016-06-15T00:00:00-05:00"

请注意,当您使用L时,您会想到您正在专门寻找日期格式'DD / MM / YYYY'。作为参考,阿拉伯沙特阿拉伯的本地化日期格式如下:

Note that when you use 'L' you are telling moment that you are specifically looking for the date format 'DD/MM/YYYY'. For reference, the Arabic Saudi Arabia localized date formats are as follows:

    LT : 'HH:mm',
    LTS : 'HH:mm:ss',
    L : 'DD/MM/YYYY',
    LL : 'D MMMM YYYY',
    LLL : 'D MMMM YYYY HH:mm',
    LLLL : 'dddd D MMMM YYYY HH:mm'

因此,无论何种格式你用作第二个参数的时刻将是寻找的格式。不同的语言环境具有不同的格式,因此在区域设置之间进行更改时需要小心。

As such, whatever format you use as the second parameter to moment will be the format moment is looking for. Different locales have different formats, so you need to be careful when changing between locales.

如果需要返回日期对象,可以执行以下操作: / p>

If you need to get back to a date object, you can do the following:

moment("١٥/٠٦/٢٠١٦", 'L').toDate()

我必须警告你,这不是Moment.js的最佳做法,除非你有第三方API需要消费日期。 Moment是一个完整的API(并且比JS Date更好),因此您需要编写的与日期和时间操作相关的任何代码都可以在当下生态系统中完成。

I must warn you though, this is not best practice in Moment.js unless you have a third party API that needs to consume a date. Moment is a complete API (and a better one than JS Date), so any code you need to write related to date and time manipulation is better done within the moment ecosystem.

这篇关于Javascript从已知语言环境的toLocaleDateString解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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