在我的应用程序中,我想根据用户的语言环境设置日期格式。如果用户修改了日期,我希望能够将其解析回 javascript Date 对象。
我想知道是否可以使用 javascript 从本地格式化的字符串中解析日期?
请考虑以下片段:
new Date(2016, 0, 1).toLocaleDateString('ar');
>>"??/??/????"
Run Code Online (Sandbox Code Playgroud)
如果我想将该字符串解析回有效日期,我将如何处理?
以下将不起作用:
new Date(new Date(2016, 0, 1).toLocaleDateString('ar'));
>>Invalid Date
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用momentjs,但无法在那里找到工作方式。
//change global locale
moment.locale('ar-sa');
moment().format('L');
>>"??/??/????"
moment(new moment().format('L'))._d;
>>Invalid Date
Run Code Online (Sandbox Code Playgroud)
或者有没有其他/更好的方式来思考这样的问题?