Skip to content Skip to sidebar Skip to footer

Displaying Proper Date Format Depending On Culture

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 con

Solution 1:

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:

Solution 2:

See toLocaleString and related functions.

Solution 3:

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

Solution 4:

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.

Solution 5:

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

Post a Comment for "Displaying Proper Date Format Depending On Culture"