Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 633
Joe Stropich Avatar asked Dec 23 '22 11:12

Joe Stropich


2 Answers

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:

  • Walkthrough: Globalizing a Date by Using Client Script
like image 172
Christian C. Salvadó Avatar answered Dec 25 '22 01:12

Christian C. Salvadó


See toLocaleString and related functions.

like image 36
jiggy Avatar answered Dec 25 '22 00:12

jiggy