toLocaleString()在不同浏览器中的行为不一致 [英] Inconsistent behavior of toLocaleString() in different browser

查看:142
本文介绍了toLocaleString()在不同浏览器中的行为不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我必须在日期和时间上处理很多事情。服务器端技术是ASP.Net,在客户端我使用jQuery和jQuery Week Calender(一个jQuery插件)。



所以这里是描述的问题,我正在接收数据时间从服务器这样的 2012-11-13T04:45:00.00 格林尼治标准格式。



现在在客户端我想要在区域设置日期时间格式中转换此日期时间,就像可以是IST,EST,PKT等一样。



为了实现这一点,我使用java脚本方法 toLocaleString(),这只能在chrome中正常工作,而在其他浏览器中它工作不正常。



以下是不同浏览器中的输出:



Google Chrome(正常工作):



致电:

 新日期(2012-11 -13T04:45:00.00)。toLocaleString(); 

输出:

 code> Tue Nov 13 2012 10:15:00 GMT + 0530(印度标准时间)

Mozilla Firefox:



致电:

  new Date(2012-11-13T04:45:00.00)。toLocaleString(); 

输出:

 code> 2012年11月13日星期二4:45:00 AM 

Safari :



致电:

  2012-11-13T04:45:00.00)。toLocaleString(); 

输出:

 code>无效日期

Internet Explorer:



致电:

 新日期(2012-11-13T04:45:00.00 ).toLocaleString(); 

输出:

 code> 2012年11月13日星期二4:45:00 AM 

现在这些是浏览器,我在哪里测试。



这是问题



强>我需要一种方式来转换数据时间(具有这样的格式 2012-11-13T04:45:00.00 )到区域设置日期和时间,无论哪个浏览器客户端使用

解决方案

简短的答案是否定的。 toLocaleString可以实现,但开发人员希望。您的问题是什么意思是Chrome输出您想要的字符串。



如果要一致输出该格式,您需要使用一个单独的图书馆,如 DateJS



要使用DateJS执行此操作将需要在core.js中可用的一些标准格式说明符,一些仅在extras.js中可用。该文档列出了所有格式说明符的列表。



您想要的字符串是:

  Tue Nov 13 2012 10 :15:00 GMT + 0530(印度标准时间)

所以要从DateJS得到这个,你会需要:

 DM d YH:i:s \G\M\TO(e)

DateJS的语法是:

 新日期(2012-11-13T04:45:00.00)格式(DM d YH:i:s \G\M\TO(e)); 


I am working on a project where I have to deal a lot with Date and Time. Server side technology is ASP.Net and at client side I am using jQuery and jQuery Week Calender(a jQuery plugin).

So here is the problem described, I am receiving Data Time from server something like this 2012-11-13T04:45:00.00 in GMT format.

Now at client side I want this Date Time to be converted in Locale Date Time Format, like whatever is could be IST, EST, PKT, etc.

To achieve this, I am using java script method toLocaleString(), This only works fine in chrome, in other browser it work Inconsistently.

Here are its outputs in different browsers:

Google Chrome(Works fine):

Call:

new Date ("2012-11-13T04:45:00.00").toLocaleString();

Output:

Tue Nov 13 2012 10:15:00 GMT+0530 (India Standard Time)

Mozilla Firefox:

Call:

new Date ("2012-11-13T04:45:00.00").toLocaleString();

Output:

Tuesday, November 13, 2012 4:45:00 AM

Safari:

Call:

new Date ("2012-11-13T04:45:00.00").toLocaleString();

Output:

Invalid Date

Internet Explorer:

Call:

new Date ("2012-11-13T04:45:00.00").toLocaleString();

Output:

Tuesday, November 13, 2012 4:45:00 AM

For now these are the browser, where I tested.

Here is the Question:

I need a way to convert Data Time(having format like this 2012-11-13T04:45:00.00) To Locale Date and Time, no matter which browser client is using.

解决方案

The short answer is no. toLocaleString can be implemented however the developers wish. What your question implies is that Chrome outputs the string you want.

If you would like to output that format consistently you'll need to use a separate library - like DateJS.

To do this with DateJS will need some standard format specifiers available in core.js and some that are only available in extras.js. The documentation has a list of all the format specifiers.

The string you want is:

Tue Nov 13 2012 10:15:00 GMT+0530 (India Standard Time)

So to get this from DateJS you'll need:

"D M d Y H:i:s \G\M\TO (e)"

The syntax for DateJS is:

new Date ("2012-11-13T04:45:00.00").format("D M d Y H:i:s \G\M\TO (e)");

这篇关于toLocaleString()在不同浏览器中的行为不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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