1

I know such questions are in ton here. Please go through once

I have a string coming from textbox having current date e.g. 10/9/2012, my class property is of DateTime? type. I am using Convert.ToDateTime(datetime_string_from_textbox) but it gives me a FormatException. I then tried DateTime.ParseExact(string, format, CultureInfo, DateTimeStyle) as suggested by Jon Skeet here but still it gave me the same exception.

One more thing — my local machine date time format is dd-mm-yyyy. When I switch this to mm/dd/yyyy format the code works fine. So basically , I want to know how to parse a valid datetime string to a DateTime object irrespective of the regional settings, or any settings or any dependency on local machine.

Is this possible?

Update : Code in use

employee.JoiningDate = DateTime.ParseExact(string.Format("{0} 00:00:00", JoiningDate.Text.Trim()), "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); 

Existing Problem and Required Solution

My system datetime shows 24-10-2012 (that is, 24th Oct) and I have 10/17/2012 in my text box (that is, 17th Oct) since the text box date is also valid and after deployment again the client datetime format will become unknown so, I want a generic way to parse any valid datetime string irrespective of regional settings. Is this possible?

Community
  • 1
  • 1
  • 1
    What format string did you use? – Bridge Oct 24 '12 at 11:32
  • 1
    what format did you specify with DateTime.ParseExact? – AdaTheDev Oct 24 '12 at 11:33
  • use mask control and restrict the input to a specific format. Use the same format while parsing the date. – jags Oct 24 '12 at 11:33
  • employee.JoiningDate = DateTime.ParseExact(string.Format("{0} 00:00:00", JoiningDate.Text.Trim()), "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); –  Oct 24 '12 at 11:33
  • The way your machine is configured, `10/9/2012` is not a valid date format, `10-9-2012` is the valid format. What business reason do you have for wanting to parse `/` separators? – Christian Hayter Oct 24 '12 at 11:34
  • @ChristianHayter My System Date time shows 24-10-2012 that is 24th Oct and I have 10/17/2012 in my text box that is 17th oct since the text box date is also valid and after deployment again the client date time format will become unknown so , I want a generic way to parse any valid datetime string irrespective of regional settings. Is this possible –  Oct 24 '12 at 11:39
  • Amit, the whole point of the system-wide international settings are to define how the user intends to enter data. If the machine is configured for D/M/Y, and the user enters M/D/Y, then that is a user training issue, and not the fault of your code. – Christian Hayter Oct 24 '12 at 11:43
  • Is there any way to avoid this –  Oct 24 '12 at 11:49

4 Answers4

5

This should work:

var date = DateTime.ParseExact(str, "M/d/yyyy", CultureInfo.InvariantCulture);

As tested bellow: 

enter image description here

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
0

Try formatting your date to international date format using this method:

How would you format DateTime in international format?

Also you can check this for your current culture:

Set Default DateTime Format c#

Community
  • 1
  • 1
Alex
  • 5,971
  • 11
  • 42
  • 80
0

It totally depends on the machine settings. DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture); will work for British format but it will give format exception on US format. So use format according to your machine settings.

Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
  • By my knowledge, `Invariant Culture` is only based on the English Language and is not culture or region specific. http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx – Silvermind Oct 24 '12 at 12:13
  • This is what I have experienced. Use US format and this code will work and use British format and this code will not work. Atleast it is with me. – Usman Khalid Oct 24 '12 at 12:29
  • I have tried this: `DateTime.ParseExact("24/12/2012", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); DateTime.ParseExact("24-12-2012", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture); DateTime.ParseExact("24.12.2012", "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);` and they all give `24-12-2012 0:00:00` So it behaves as expected, nothing wrong. My native settings are `dd-MM-yyyy`. – Silvermind Oct 24 '12 at 14:02
  • `12.24.2012 --> MM.dd.yyyy` and `12/24/2012 --> MM/dd/yyyy` and `12-24-2012 --> MM-dd-yyyy` same results – Silvermind Oct 24 '12 at 14:04
0

Try the following if it works

var formatInfo = new DateTimeFormatInfo();
formatInfo.ShortDatePattern = "MM/dd/yyyy";
DateTime.Parse(date, formatInfo); 
pmtamal
  • 2,157
  • 1
  • 12
  • 7