0

I want to accept date in dd-MM-yyyy format using textbox I have used ajax calendar also.

DateTime.ParseExact(txtDate.Text,"dd-MM-yyyy",null) 

and

Convert.ToDateTime(txtDate.Text) 

are both throwing exception:

string was not recognized as valid datetime.

I know when I will change my system dateformat which is currently MM-dd-yyyy to dd-MM-yyyy, it will start recognizing it but what is solution when I will publish it on server.

So is there any solution to parse it for current format ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
donstack
  • 2,557
  • 3
  • 29
  • 44
  • 2
    Hm, what is your actual value in the textbox? Maybe it's simply doesn't match the format? If not, try specifying `CultureInfo.InvariantCulture` as the 3rd parameter of ParseExact. – Vlad Mar 03 '13 at 13:56
  • when we should use null and when InvariantCulture – donstack Mar 03 '13 at 14:07
  • If I am not mistaken, `null` means "take the current system language", whereas `InvariantCulture` basically means "make no language-specific substitutions". – Vlad Mar 03 '13 at 14:08
  • What **exactly** are your users entering into that textbox? Something like `19-05-2012` ? This works just fine for me. Or are the users entering some other format..... – marc_s Mar 03 '13 at 14:55

2 Answers2

0

You need to pass an IFormatProvider parameter to the ParseExact method like :

CultureInfo provider = CultureInfo.GetCultureInfo("en-US")
DateTime.ParseExact(txtDate.Text,"dd-MM-yyyy",provider) 

And don't forget to use the System.Globalization namespace

Axel
  • 361
  • 2
  • 6
  • Everyone is giving different solutions but which one should be used. i am confused with these different methods from a long time. – donstack Mar 03 '13 at 14:25
  • Basically, with the formatprovider you are specifying what format should the method use to recognize the string you are passing. By default, the system's format is used (if you pass the null value), that's why when you change it in the system, it works. The answers given just cover different situations. My answer simply sets an specific culture, US ("en-US"). – Axel Mar 03 '13 at 14:29
  • Here is a post about when to use the invariant culture, i think it explains it nicely http://stackoverflow.com/questions/2423377/what-is-the-invariant-culture – Axel Mar 03 '13 at 14:31
  • Which method you use depends on what you want to do with it. If you only care about a specific format, then use CultureInfo.GetCultureInfo("en-US") or CultureInfo.GetCultureInfo("en-GB") or whatever you want. If you want your system to set the value to whatever your users' settings are, then use the method in my answer which uses the `CurrentCulture` from `System.Threading.Thread.CurrentThread`. – Matt Mar 03 '13 at 14:54
  • One question Matt, doesn't CurrentThread return the application's process culture, meaning that if you haven't changed it before, it will always return the current system's culture? – Axel Mar 03 '13 at 14:57
0

You need to use System.Threading.Thread.CurrentThread.CurrentCulture to get the user's current settings. Then use that to get the formatted date string.

DateTime now = DateTime.Now;
string formattedToCurrentUser = now.ToString(System.Threading.Thread.CurrentThread.CurrentCulture);
Matt
  • 6,787
  • 11
  • 65
  • 112