ToLocaleDateString() 在 IE11 中的变化 [英] ToLocaleDateString() changes in IE11

查看:34
本文介绍了ToLocaleDateString() 在 IE11 中的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 IE 11 中,我使用 ToLocaleDateString() 得到了有趣的结果.返回的字符串在浏览器中看起来不错,例如1/28/2014 11:00:46 AM",但是如果我将该值复制并粘贴到纯文本编辑器中,它看起来像这样:?1?/?28?/?2014?11?:?00?:?46??AM".

In IE 11, I'm getting funny results with ToLocaleDateString(). The string returned looks fine in the browser, e.g. "1/28/2014 11:00:46 AM", but then if I copy and paste that value into a plain text editor, it looks like this: "?1?/?28?/?2014 ?11?:?00?:?46? ?AM".

有趣的是,如果我将文本粘贴到 Microsoft 产品中,它看起来不错...问题是,如果您尝试以编程方式使用该值来创建日期,则它无效.您可以通过在 IE11 中打开控制台并在其上使用 ToLocaleDateString() 创建一个新日期来测试这一点,然后尝试使用结果字符串在 javascript 或您选择的语言中创建一个新日期(我'我在这里使用 ASP.NET...).

Interestingly, if I paste the text into a Microsoft product, it looks fine... The issue is that if you try to use the value programmatically to create a date, it's invalid. You can test this by just opening up a console in IE11 and creating a new date, using ToLocaleDateString() on it, and then trying to use the resulting string to create a new date in javascript or in the language of your choice (I'm using ASP.NET here...).

我是不是做错了什么,还是我应该通过其他方式与 javascript Date 交互?我怎样才能摆脱那些时髦的符号?

Am I doing something wrong, or is there some other way I'm supposed to be interacting with the javascript Date? How can I get rid of those funky symbols?

感谢下面的评论,我能够弄清楚未显示的字符是什么,它们是从左到右的标记.根据我将值粘贴到的编辑器以及编辑器设置使用的编码,文本的显示方式会有所不同:有时带有?",有时没有.

Thanks to the comment below I was able to figure out what the unshown characters are, they're Left-To-Right marks. Depending on the editor I paste the values into and the encoding that the editor is set to use, the text will show up differently: sometimes with "?", sometimes without.

推荐答案

问题在于,如果您尝试以编程方式使用该值来创建日期,则它是无效的.

The issue is that if you try to use the value programmatically to create a date, it's invalid.

...

我是不是做错了什么,还是我应该通过其他方式与 javascript 日期交互?

Am I doing something wrong, or is there some other way I'm supposed to be interacting with the javascript Date?

是的,你做错了.您不应该使用旨在为特定于语言环境的人类显示格式化某些内容的函数,并期望输出是机器可解析的.toLocaleStringtoLocaleDateStringtoLocaleTimeString 的任何输出仅用于人类可读的显示.(正如 Bergi 在评论中澄清的那样,toString 也用于人类显示,但 ECMA §15.9.4.2 说它应该往返)

Yes, you are doing it wrong. You shouldn't be using a function intended to format something for locale-specific human display and expect the output to be machine parsable. Any of the output of toLocaleString, toLocaleDateString, or toLocaleTimeString are meant for human-readable display only. (As Bergi clarified in comments, toString is also meant for human display, but ECMA §15.9.4.2 says it should round-trip)

您可能会获得 LTR 标记,因为您的显示区域设置是 RTL.除此之外,请考虑语言环境将始终影响输出.也许您的语言环境使用 dd/mm/yyyy 格式而不是 mm/dd/yyyy 格式.或者,您的语言环境可能需要亚洲或阿拉伯字符.这些都是确定显示格式时的考虑因素,但并不适合机器解析.

You are likely getting the LTR markers because your display locale is RTL. Besides this, consider that the locale will always affect the output. Perhaps your locale uses dd/mm/yyyy formatting instead of mm/dd/yyyy formatting. Or perhaps your locale requires Asian or Arabic characters. These are all considerations when determining a display format, but are never appropriate for machine parsing.

还要考虑一下,ECMAScript 规范并没有为这些方法的输出定义任何特定的格式规则,不同的浏览器会产生不同的结果.

Also consider that the ECMAScript specification does not define any particular formatting rules for the output of these methods, and different browsers will yield different results.

如果意图不是向用户显示,那么您应该使用以下功能之一:

If the intent is anything other than to display to the user, then you should use one of these functions instead:

  • toISOString 会给你一个 ISO8601/RFC3339 格式的时间戳
  • toGMTStringtoUTCString 会给你一个 RFC822/RFC1123 格式的时间戳
  • getTime 会给你一个毫秒精度的整数 Unix 时间戳
  • toISOString will give you an ISO8601/RFC3339 formatted timestamp
  • toGMTString or toUTCString will give you an RFC822/RFC1123 formatted timestamp
  • getTime will give you an integer Unix Timestamp with millisecond precision

以上所有内容都将返回一个基于 UTC 的值.如果需要本地时间,可以使用各种访问器函数(getFullYeargetMonth 等)构建自己的字符串,也可以使用库比如moment.js:

All of the above will return a UTC-based value. If you want the local time, you can either build your own string with the various accessor functions (getFullYear, getMonth, etc...), or you can use a library such as moment.js:

这使用 moment.js 返回 ISO8601 格式的本地时间 + 日期偏移量:

This uses moment.js to return an ISO8601 formatted local time + offset from a date:

moment(theDate).format()   // ex:  "2014-08-14T13:32:21-07:00"

这篇关于ToLocaleDateString() 在 IE11 中的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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