我正在使用弹出日历日期选择器的控件。 这使用 JavaScript 函数 SetText 将文本框设置为给定日期。 我无法更改日历控件本身的任何内容,但我可以覆盖 SetText 函数。 SetText javascript 仅采用字符串格式的 TextBox 名称和日期值,并将 TextBox 设置为该字符串。
问题:
我需要以“4 月 30 日”的格式显示日期。
容易做。 使用 getMonth() 和 getDate() 我可以从那里解析信息。
现在,我需要确保这对于不同的文化能够正确显示。 例如,英国将日期显示为“4 月 30 日”。 由于代码隐藏(c#)可以以英国格式发送日期,我如何在 JavaScript 中知道他们使用的是英国(dd/mm/yyyy)而不是美国(mm/dd/yyyy)?
浏览器导航器语言可能设置为一种设置,而服务器设置为另一种设置,从而导致 1 月 4 日与 4 月 1 日不匹配。
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.
The problem:
I need to display the date in the format "April 30".
Easy to do. Use getMonth() and getDate() where I can parse the information from there.
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.
发布评论
评论(8)
您正在使用Microsoft Ajax框架,该框架定义了一组“客户端类型扩展”,为 JavaScript 基本类型提供附加功能或“扩展”。
日期类型扩展是您正在寻找的,特别是Date.parseLocale 函数。
使用此函数,您可以使用给定的格式解析字符串。
您可以通过设置 ScriptManager.EnableScriptGlobalization 属性设置为 true,并使用 Date.parseLocale 函数而不指定任何格式。
查看这篇文章:
You are using the Microsoft Ajax Framework, this framework defines a set of "client-side type extensions" which provide added functions or "extensions" to the JavaScript base types.
The Date Type Extensions is what you're looking for, specifically the Date.parseLocale function.
With this function you can parse a string, using a given format.
You can synchronize your server-side and client-side culture by setting the ScriptManager.EnableScriptGlobalization property to true, and use the Date.parseLocale function without specifying any format.
Give a look to this article:
请参阅 toLocaleString 和相关函数。
See toLocaleString and related functions.
如果您控制后端,为什么不直接发送时间戳并将其推送到
Date
对象中?至于客户端的格式化,由于我已经在使用Dojo,所以我通过使用
dojo.date.locale.format
解决了这个问题。 完全无痛。教程:http://docs.dojocampus.org/dojo/date/locale< br>
API文档:
http://api.dojotoolkit.org/jsdoc/1.3/dojo。日期.区域设置.格式
日期格式说明: http://www.unicode.org/reports/ tr35/tr35-4.html#Date_Format_Patterns
If you control the backend, why not just send a timestamp and push it into
Date
object?As for formatting on the client side, since I was already using Dojo, I solved this problem by using
dojo.date.locale.format
. It was completely painless.Tutorial: http://docs.dojocampus.org/dojo/date/locale
API doc:
http://api.dojotoolkit.org/jsdoc/1.3/dojo.date.locale.format
Date format descriptions: http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
您可以使用三件事:
1) toLocaleString - 正如已经建议的那样。 问题是当发送“4/1/2009”字符串时,这可能会导致一些结果。 1 月 4 日或 4 月 1 日。
2) navigator.language 和 navigator.systemLanguage - 在您之后获取日期字符串,您可以检查系统使用的语言并从那里解析日期。 这个和解决方案 1 的问题是,如果您有一个英国服务器并且浏览器计算机是美国的。 您将获得将 4 月 1 日发送为 1/4/2009 的代码,其中 javascript 将以客户端浏览器使用的任何语言读取该字符串。 因此,英国服务器和美国浏览器会给你一个错误的结果。
3) 使用代码隐藏文化 - 在 JavaScript 中创建一个变量,当页面加载时,它将调用代码隐藏中的函数,从那里返回
this.Page.Culture
,您将知道什么文化该字符串被发送回。 这将消除前两个解决方案可能导致的不匹配。 需要一些额外的工作来确保它正确显示,但至少您将能够使用该字符串,而不会出现区域性不匹配的可能性。Three things you could use:
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 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) 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.toLocaleDateString 将是比 toLocaleString 解决您的问题,因为它不包括时间(因为您只要求日期)。
toLocaleDateString would be a better solution than toLocaleString for your problem as it doesn't include the time (as you only are requesting the date).
开源 JavaScript 库 Date.js 有一些很棒的格式化日期的方法,并且它支持多种语言:
Google Code 上的 Date.js:http://code.google.com/p/datejs/
如果您想要格式良好的日期/时间,只需传递 格式化字符串(几乎与 .NET Framework 中使用的字符串相同)插入任何 Date 对象的
.toString( )
方法。它还具有一整套文化,使您可以简单地包含适合该文化的脚本。
如果您想自己管理(就像我们在应用程序中所做的那样),您可以找到一些资源,为您提供适合给定区域性的资源字符串列表。 下面是显示大量文化的正确格式字符串的一个: http://www.transactor.com /misc/ranges.html
The open-source JavaScript library Date.js has some great methods for formatting dates, as well as it supports a bunch of languages:
Date.js at Google Code: http://code.google.com/p/datejs/
If you want nicely formatted dates / times, you can just pass a formatting string (nearly identical to those used in .NET Framework) into any Date object's
.toString()
method.It also has a whole set of cultures which allow you to simply include the appropriate script for that culture.
If you want to manage that yourself (as we do in our apps), you can find resources which give you the list of appropriate resource strings for a given culture. Here's one that shows proper formatting strings for a ton of cultures: http://www.transactor.com/misc/ranges.html
当您使用 ASP.NET 时,您也可能使用 ASP.NET Ajax。 如果是这样,则 ScriptManager 对您有用的:
EnableScriptLocalization - 获取或设置一个值,该值指示 ScriptManager 控件是否呈现脚本文件的本地化版本。
EnableScriptGlobalization - 获取或设置指示 ScriptManager 控件呈现的脚本是否支持特定区域性信息的解析和格式化的值。
当您启用这两个选项(设置为 true)时,ASP.NET Ajax 扩展程序等应自动本地化为 web.config 中指定的区域性:
例如,设置此项将本地化 AjaxControlToolkit 日历 融入您的特定文化。
即使您不使用 ASP.NET Ajax,添加 ScriptManager 并启用本地化也会为您提供一个名为 __cultureInfo 的有用 javascript 变量,其中包含本地化格式的 JSON 数组,例如货币、日期等。
As you are using ASP.NET then you may also be using ASP.NET Ajax. If so, there are two properties on the ScriptManager that are of use to you:
EnableScriptLocalization - Gets or sets a value that indicates whether the ScriptManager control renders localized versions of script files.
EnableScriptGlobalization - Gets or sets a value that indicates whether the ScriptManager control renders script that supports parsing and formatting of culture-specific information.
When you enable both of these (set to true) then ASP.NET Ajax extenders etc. should automatically be localised into the culture specified in web.config:
For instance, setting this will localise the AjaxControlToolkit Calendar into your specificed culture.
Even if you are NOT using ASP.NET Ajax adding a ScriptManager and enabling localisation will give you a useful javascript variable called
__cultureInfo
that contains a JSON array of localised formate, such as currencies, dates etc.我通过使用 Datejs 解决了这个问题,
(在您的情况下,您可以从 navigator.systemLanguage (或 navigator.browserLanguge 等)获取区域性,并将脚本标记添加到标头,其中 src 属性指向适当的路径)
其中 d 是任何日期对象
(我尝试使用 Date.today().toShortDateString() 但它抛出异常。(CultureInfo JSON 对象的结构与函数预期的结构不同)。
I solved this problem by using Datejs as
(in your case you can get the culture from navigator.systemLanguage (or navigator.browserLanguge etc) and add a script tag to the header with src attribute pointing to the appropriate path)
where d is any date object
(I tried using Date.today().toShortDateString() but it was throwing exception. (the CultureInfo JSON object had a different structure than what the function expects).