views:

52

answers:

2

Hi I am getting DateString in JavaScript from the TextBox like,

    var dateString = document.getElementById('<%= txtEffDate.ClientID %>').value;

dateString value may be in following format.

en-US -> "4/29/2010"

fr-FR -> "29/04/2010"

de-DE -> "29.04.2010"

it-IT -> "29/04/2010"

es-ES -> "29/04/2010"

zh-CN -> "2010/4/29"

ja-JP -> "2010/04/29"

sv-SE -> "2010-04-29"

And converting this to Date Object as follows,

var d1 = new Date(dateString);

though its giving me the wrong result like for fr-FR "29/04/2010" ==> "Fri May 4 00:00:00 UTC+0530 2012"

Which is completely irrelevant, How should I get the correct Date ?

I have also set < globalization culture="auto" /> in web.config & < asp:ScriptManager ID="scr1" runat="server" EnableScriptGlobalization="true"/>

A: 

Natively, Javascript has very, very limited ability to interpret date strings. Your best bet is to use an add-on library to do it, such as DateJS, or to send the string to the server and use .Net's date processing there.

T.J. Crowder
A: 

First, setting < globalization culture="auto" /> in web.config does not work, JavaScript runs on client side.

If you know the current culture on client side, then you can parse out Year, Month, Day from the text value of txtEffDate textbox. then you can use this code to contruct Date object.

var d = new Date();
d.setYear(2009);
d.setMonth(11);
d.setDate(2);
d.toString(); //"Wed Dec 02 2009 16:57:51 GMT-0500 (Eastern Standard Time)"
Ding