根据文化显示正确的日期格式 [英] Displaying proper date format depending on culture

查看:351
本文介绍了根据文化显示正确的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个弹出的日历日期选择器控制。这将使用JavaScript函数的setText,到文本框设置为给定的日期。我不能在日历控件改变任何东西本身,而是我可以覆盖的setText功能。在JavaScript的的setText只需要文本框名称和字符串格式的日期值,并设置文本框的字符串。

I am using a control for a popup calendar date picker. This uses a javascript function, SetText, to set the textbox to the given date. I can't change anything in the calendar control itself but I can override the SetText function. The SetText javascript just takes the TextBox name and the date value in string format and sets the TextBox to the string.

问题:
我需要显示格式为4月30日的日期。

The problem: I need to display the date in the format "April 30".

容易做到的。使用的getMonth()和GETDATE(),我可以从那里解析信息。

Easy to do. Use getMonth() and getDate() where I can parse the information from there.

现在,我需要确保这个正确显示了不同的文化。例如,英国的显示日期为4月30日。由于code-背后(C#)可发送的日期在英国格式我怎么知道他们正在使用英国(日/月/年)的JavaScript,而不是美国(MM / DD / YYYY) ?

而服务器设置为另一个导致1月4日为4月1日不匹配的浏览器导航语言可以设置为一个设置。

Now, I need to make sure this shows correctly for different cultures. For example, the UK shows dates as "30 April". Since the code-behind(c#) could be sending the date in the UK format how do I know in the javascript that they're using UK(dd/mm/yyyy) and not US(mm/dd/yyyy)?
The browsers navigator language could be set to one setting while the server is set to another to cause a January 4 as April 1 mismatch.

推荐答案

您可以用三件事情:

1)的toLocaleString - 作为已经建议。发送的2009/4/1的字符串时,这个问题是这样可以导致一对夫妇的事情。 1月4日或4月1日

1) toLocaleString - As suggested already. The problem with this is when sending a string of "4/1/2009" this can result in a couple things. January 4 or April 1.

2)navigator.language和 navigator.systemLanguage - 在你获取日期字符串你可以检查系统是什么语言,并从那里解析日期。这种解决方案和1是什么问题,如果你有一个英国的服务器和浏览器的机器是美国。您将有落后发送4月1日为2009年1月4日在那里的JavaScript将读取字符串作为任何一种语言的客户端浏览器是code。因此,英国的服务器和浏览器我们会给你一个错误的结果。

2) navigator.language and navigator.systemLanguage - After you get the date string you can check to see what language the system is in and parse the date from there. The problem with this and solution 1 is what if you have a UK server and the browsers machine is US. You will have the code behind sending April 1 as 1/4/2009 where the javascript will read the string as whatever language the clients browsers is. So, UK server and US browser will give you a wrong result.

3)用code屁股文化 - 在JavaScript中创建一个变量,在页面加载时,它会调用一个函数在后面,你的code返回 this.Page.Culture 从那里,你就会知道正在发送的文化串向后。这将消除不匹配的,前两个解可引起。这将需要一些额外的工作,以确保它的正常显示,但至少你将能够使用的字符串,而无需文化不匹配的可能性。

3) Use Code Behinds Culture - Create a variable in your javascript that when the page loads, it will call a function in your code behind that returns this.Page.Culture from there, you will know what culture the string is being sent back as. This will eliminate the mismatch that the first two solutions can cause. It will take a little extra work to make sure it's displayed correctly but at least you will be able to use the string without having the possibility of mismatching cultures.

这篇关于根据文化显示正确的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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