ToLocaleDateString()在IE11中更改 [英] ToLocaleDateString() changes in IE11

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

问题描述

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



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



我做错了,还有其他方式我应该和JavaScript Date交互?如何摆脱那些时髦的符号?



编辑:
感谢下面的评论,我能够找出什么未示出的字符是,它们是从左到右的标记。根据编辑器,我将值和编辑器设置为使用的编码进行粘贴,文本将会出现不同的显示方式:有时候会出现?,有时没有。

解决方案


问题是如果您尝试以编程方式使用该值创建日期,则无效。



...



我做错了,还有其他一些我应该要与JavaScript日期交互?


是的,你做错了。您不应该使用旨在格式化特定于人机界面的人机界面的功能,并期望输出是机器可解析的。 toLocaleString toLocaleDateString toLocaleTimeString 的任何输出仅用于人类可读显示。 (正如Bergi在评论中澄清的, toString 也用于人类显示,但 ECMA§15.9.4.2表示应该往返)



您可能会收到LTR标记,因为您的显示区域是RTL。此外,考虑到区域设置将始终影响输出。也许您的区域设置使用dd / mm / yyyy格式,而不是mm / dd / yyyy格式。或者您的地区需要亚洲或阿拉伯字符。这些都是确定显示格式时的注意事项,但不适用于机器分析。



还要考虑到ECMAScript规范未定义任何特定的格式化规则这些方法和不同的浏览器将产生不同的结果。



如果意图是向用户显示的其他内容,则应该使用其中一个函数: / p>


  • toISOString 将为您提供ISO8601 / RFC3339格式的时间戳

  • toGMTString toUTCString 将为您提供RFC822 / RFC1123格式的时间戳

  • getTime 将给你一个整数Unix Timestamp,毫秒精度



以上所有都将返回基于UTC的值。如果你想要当地时间,你可以使用各种访问器函数( getFullYear getMonth 构建自己的字符串,等等),或者您可以使用库,例如​​ moment.js



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

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


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".

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...).

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?

Edit: 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.

...

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

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)

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.

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 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

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:

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天全站免登陆