DateTime.TryParse
-
Any idea how to convert strings like "Tuesday 1st April 2008" to a DateTime. I have tried DateTime.TryParse with different CultureInfo, without result Kjetil
-
Any idea how to convert strings like "Tuesday 1st April 2008" to a DateTime. I have tried DateTime.TryParse with different CultureInfo, without result Kjetil
You can use DateTime.ParseExact Method [^]
Giorgi Dalakishvili #region signature my articles #endregion
-
You can use DateTime.ParseExact Method [^]
Giorgi Dalakishvili #region signature my articles #endregion
Thanks Useful tip, but I was not able parse the date DateTime dt = DateTime.ParseExact("April 1st 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 1 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 01 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Succeeded Any idea what formatstring to use ? I could manipulate the string before parsing, but I'm parsing 8 different languages with different date format :-/ Kjetil
-
Thanks Useful tip, but I was not able parse the date DateTime dt = DateTime.ParseExact("April 1st 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 1 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 01 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Succeeded Any idea what formatstring to use ? I could manipulate the string before parsing, but I'm parsing 8 different languages with different date format :-/ Kjetil
I don't think it's possible to have one generic format for the above examples. Either some manipulation and FormatException handling is going to be required, or you're going to have to force them in your UI to enter data in an acceptable format by either separating the day, month and year fields into separate controls or using a DateTimePicker or something similar. The second two examples shouldn't be too hard, but the date suffix in the first does not appear to be included in any of the custom format mechanisms - unless it's possible to use a wild card for the two characters? It may be easier to create your own control that can handle the 8 different language formats - that will give you more flexibility if more are required later.
Dave
-
I don't think it's possible to have one generic format for the above examples. Either some manipulation and FormatException handling is going to be required, or you're going to have to force them in your UI to enter data in an acceptable format by either separating the day, month and year fields into separate controls or using a DateTimePicker or something similar. The second two examples shouldn't be too hard, but the date suffix in the first does not appear to be included in any of the custom format mechanisms - unless it's possible to use a wild card for the two characters? It may be easier to create your own control that can handle the 8 different language formats - that will give you more flexibility if more are required later.
Dave
Guess I have to do some string-manipulation. My app reads the dates from different sources, like word, excel, text or xml files, so it's not possible to solve this by UI changes... Thanks Kjetil