0

I have multiple datetime values. I would like to convert them to datetime with one method.

The date values may change by system regional settings like below.

'7/26/2013 12:00:00 AM'
'26.7.2013 12:00:00'
'07-26-2013 12:00 AM'

Is there a way to do this without changing system regional settings with one method?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
  • [DateTime.TryParse(...)](http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx) ? as per the doc's `DateTime.TryParse(String, DateTime) method tries to parse the string representation of a date and time using the formatting rules of the current culture.` – Viv Jul 26 '13 at 01:34
  • I have tried DateTime.TryParse, but it didn't work. – Sinan AKYAZICI Jul 26 '13 at 01:43
  • possible duplicate of [DateTime.TryParse all possible type of dates](http://stackoverflow.com/questions/2326127/datetime-tryparse-all-possible-type-of-dates) – nawfal Jan 30 '14 at 08:04

1 Answers1

1

I believe the question DateTime.TryParse all possible type of dates may be of some use to you.

Here is the snippet:

CultureInfo ci = CultureInfo.GetCultureInfo("sl-SI");
string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();

if (DateTime.TryParseExact(date, fmts, ci, DateTimeStyles.AssumeLocal, out dt))
{
    DateTime = Convert.ToDateTime(date);
    Check = true;
}

Update:

This codeproject article may also be of use to you:

http://www.codeproject.com/Articles/33298/C-Date-Time-Parser

Community
  • 1
  • 1
jrbeverly
  • 1,611
  • 14
  • 20
  • Thanks for your answer. I dont know culture info. That may change. Should I use CultureInfo.CurrentCulture ? – Sinan AKYAZICI Jul 26 '13 at 01:44
  • I would use CultureInfo.CurrentCulture, for most cases that should work just fine. http://www.csharp-examples.net/culture-names/ Also provides a list of culture names in case you need em – jrbeverly Jul 26 '13 at 02:07