Display the correct date format depending on the culture

I am using a control to select a date in a popup calendar. This uses the javascript SetText function to set the text field to a given date. I can’t change anything in the calendar control, but I can override the SetText function. The SetText JavaScript script simply takes the name of the TextBox and the date value in a string format and sets the TextBox to a string.

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 that this is correctly shown for different cultures. For example, the UK shows dates as April 30th. Since the code behind (C #) can send a date in the UK format, as I know in javascript, that they use the UK (dd / mm / yyyy) and not the US (mm / dd / yyyy)?
The language of the browser navigator can be set to one parameter when the server is configured to another, so that on January 4 there will be a mismatch on April 1.

+4
source share
8 answers

See toLocaleString and related functions.

+2
source

If you control the backend, why not just send a timestamp and paste it into a Date object?

Regarding client-side formatting, since I already used Dojo, I solved this problem using dojo.date.locale.format . It was completely painless.

  • The locale is automatically detected or can be set arbitrarily.
  • Formatting options (ex: long short)
  • Data selectors (e.g. time, date)
  • The ability to specify an arbitrary date / time template (perhaps not an application for 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 description: http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

+2
source

Three things you could use:

1) toLocaleString - As already mentioned. The problem is that when sending the string "4/1/2009" this can lead to a couple of things. January 4 or April 1.

2) navigator.language and navigator.systemLanguage - After you get the date string, you can check what language the system is in and analyze the date from there. The problem with this and solution 1 is that if you have a server in the UK and a browser machine in the USA. You will have the code sent April 1 as 1/4/2009, where javascript will read the line like any language in which client browsers work. Thus, the UK server and the US browser will give you the wrong result.

3) Use Code Behinds Culture - create a variable in your javascript that when you load the page will call a function in your code that returns this.Page.Culture , from where you will know what culture the line sends in the form. This will eliminate the inconsistency that the first two solutions may cause. This will require a bit of extra work to make sure that it displays correctly, but at least you can use the string without the possibility of crop mismatch.

+1
source

toLocaleDateString would be a better solution than toLocaleString for your problem, since it does not include time (since you only ask for the date).

+1
source

The open-source JavaScript library Date.js has great methods for formatting dates, and also supports many languages:

Date.js on Google Code: http://code.google.com/p/datejs/

If you want beautifully formatted dates / times, you can simply pass the format string (almost identical to the one used in the .NET Framework) into any Date .toString() object method.

It also has a whole set of cultures that allow you to simply include the appropriate script in it.

If you want to manage this yourself (as in our applications), you can find resources that give you a list of relevant resource strings for a given culture. Here's the correct line format for a ton of crops: http://www.transactor.com/misc/ranges.html

+1
source

As you use ASP.NET, you can also use ASP.NET Ajax. If so, there are two properties on ScriptManager :

EnableScriptLocalization - Gets or sets a value indicating whether the ScriptManager control displays localized versions of script files.

EnableScriptGlobalization - Gets or sets a value indicating whether the ScriptManager control supports a script that supports parsing and formatting of culture, specific information.

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

If you enable both of them (set to true), then ASP.NET Ajax extenders, etc. should automatically be localized in 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 example, setting this will localize the AjaxControlToolkit Calendar to your particular culture.

Even if you are NOT using ASP.NET Ajax, adding ScriptManager and turning on localization will give you a useful javascript variable called __cultureInfo that contains a JSON array of localized 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.... 
0
source

I solved this problem using Datejs as

  • In codebehind (aspx.cs) I get the culture for the 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 the src attribute pointing to the corresponding 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 threw an exception (CultureInfo JSON object had a different structure than the function expected).

0
source

All Articles