根据文化显示正确的日期格式

发布于 2024-07-17 16:41:39 字数 471 浏览 5 评论 0 原文

我正在使用弹出日历日期选择器的控件。 这使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

难理解 2024-07-24 16:41:39

您正在使用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:

孤独患者 2024-07-24 16:41:39

请参阅 toLocaleString 和相关函数。

See toLocaleString and related functions.

乙白 2024-07-24 16:41:39

如果您控制后端,为什么不直接发送时间戳并将其推送到 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.

  • Locale is detected automatically or can be set arbitrarily.
  • Shorthand format options (e.g.: long short)
  • Data selectors (e.g.: time, date)
  • Ability to specify an arbitrary date/time pattern (probably not application to this application, but still useful).

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

纸短情长 2024-07-24 16:41:39

您可以使用三件事:

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.

旧城空念 2024-07-24 16:41:39

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

哑剧 2024-07-24 16:41:39

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

两人的回忆 2024-07-24 16:41:39

当您使用 ASP.NET 时,您也可能使用 ASP.NET Ajax。 如果是这样,则 ScriptManager 对您有用的:

EnableScriptLocalization - 获取或设置一个值,该值指示 ScriptManager 控件是否呈现脚本文件的本地化版本。

EnableScriptGlobalization - 获取或设置指示 ScriptManager 控件呈现的脚本是否支持特定区域性信息的解析和格式化的值。

<asp:ScriptManager ID="AjaxManager" runat="Server" EnablePartialRendering="true"
            EnableScriptGlobalization="true" EnableScriptLocalization="true" />

当您启用这两个选项(设置为 true)时,ASP.NET Ajax 扩展程序等应自动本地化为 web.config 中指定的区域性:

<configuration>
  <system.web>
    <globalization 
       fileEncoding="utf-8" 
       requestEncoding="utf-8" 
       responseEncoding="utf-8" 
       culture="en-GB" 
       uiCulture="en-GB" />  
  </system.web>
</configuration>

例如,设置此项将本地化 AjaxControlToolkit 日历 融入您的特定文化。

即使您不使用 ASP.NET Ajax,添加 ScriptManager 并启用本地化也会为您提供一个名为 __cultureInfo 的有用 javascript 变量,其中包含本地化格式的 JSON 数组,例如货币、日期等。

"CalendarType":1,"Eras":[1],"TwoDigitYearMax":2029,"IsReadOnly":true},"DateSeparator":"/","FirstDayOfWeek":1,"CalendarWeekRule":0,"FullDateTimePattern":"dd MMMM yyyy HH:mm:ss","LongDatePattern":"dd MMMM yyyy","LongTimePattern":"HH:mm:ss","MonthDayPattern":"dd MMMM","PMDesignator":"PM","RFC1123Pattern":"ddd, dd MMM yyyy HH\u0027:\u0027mm\u0027:\u0027ss etc....

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.

<asp:ScriptManager ID="AjaxManager" runat="Server" EnablePartialRendering="true"
            EnableScriptGlobalization="true" EnableScriptLocalization="true" />

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:

<configuration>
  <system.web>
    <globalization 
       fileEncoding="utf-8" 
       requestEncoding="utf-8" 
       responseEncoding="utf-8" 
       culture="en-GB" 
       uiCulture="en-GB" />  
  </system.web>
</configuration>

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.

"CalendarType":1,"Eras":[1],"TwoDigitYearMax":2029,"IsReadOnly":true},"DateSeparator":"/","FirstDayOfWeek":1,"CalendarWeekRule":0,"FullDateTimePattern":"dd MMMM yyyy HH:mm:ss","LongDatePattern":"dd MMMM yyyy","LongTimePattern":"HH:mm:ss","MonthDayPattern":"dd MMMM","PMDesignator":"PM","RFC1123Pattern":"ddd, dd MMM yyyy HH\u0027:\u0027mm\u0027:\u0027ss etc....
病女 2024-07-24 16:41:39

我通过使用 Datejs 解决了这个问题,

  • 在代码隐藏(aspx.cs)中,我获取了员工的文化,并将适当的 js 添加到标题中,如下所示

字符串路径=
"http://datejs.googlecode.com/svn/trunk/build/date-
+ GetCulture() + ".js"; Helper.AddJavaScript(this, 路径);

(在您的情况下,您可以从 navigator.systemLanguage (或 navigator.browserLanguge 等)获取区域性,并将脚本标记添加到标头,其中 src 属性指向适当的路径)

  • 在客户端我使用

d.toString(Date.CultureInfo.formatPatterns.shortDate)

其中 d 是任何日期对象
(我尝试使用 Date.today().toShortDateString() 但它抛出异常。(CultureInfo JSON 对象的结构与函数预期的结构不同)。

I solved this problem by using Datejs as

  • In codebehind(aspx.cs) I get the culture for an employee and add the appropriate js to the header as

string path =
"http://datejs.googlecode.com/svn/trunk/build/date-"
+ GetCulture() + ".js"; Helper.AddJavaScript(this, path);

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

  • On the client-side I use

d.toString(Date.CultureInfo.formatPatterns.shortDate)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文