-1

I have a method that parse a date string

public static DateTime ParseDateTime(string dateString)
{
        DateTime dateTime;
        if (!DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime))
        {
            try
            {
                dateTime = DateTime.Parse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            }
            catch (FormatException)
            {
                ...
            }

        dateTime = dateTime.ToUniversalTime();
        return dateTime;
}   

But in input I can have a different formats, for example if I change date format to d/MM/yyyy(Australia and United Kingdom locales) in my GUI then I will have System.FormatException Additional information: String was not recognized as a valid DateTime.

How can I handle both of the situations?

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Anatoly
  • 1,908
  • 4
  • 25
  • 47

4 Answers4

3

You need to use ParseExact method instead of Parse and provide format string according to format you're using.

If you need to handle multiple formats at the same time, you can specify multiple formats in formats[] array.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Can you provide a code? I dont now how can I put multiple formats at the DateTime.ParseExact() method? – Anatoly Aug 26 '15 at 09:04
  • Have you had a look to the content of link I've posted? There is an example at the end of the article. – Andrey Korneyev Aug 26 '15 at 09:18
  • Yes, in MSDN all formats started with `M`, but United Kingdom locale is `d/MM/yyyy`. Where I can find list of all formats? – Anatoly Aug 26 '15 at 09:23
  • 1
    @Anatoly There are no such thing as "list of all possible formats" since you can construct format string by yourself using custom format specifiers. Have a look here: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx – Andrey Korneyev Aug 26 '15 at 09:27
  • Can I have DateTimeStyles.AssumeUniversal ? – Anatoly Aug 26 '15 at 10:49
  • It's up to you since I don't know if your date string contains timezone information or not. – Andrey Korneyev Aug 26 '15 at 10:59
2

You can try like this:

DateTime d1 = DateTime.ParseExact("2015-08-26 10:34:50,431", "yyyy-MM-dd HH:mm:ss,fff",                                                                                                
               System.Globalization.CultureInfo.InvariantCulture)

Check out DateTime.ParseExact Method for details.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Given that you know the current culture, you can use this:

DateTime.Parse(dateString, new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, false));
Marcus
  • 8,230
  • 11
  • 61
  • 88
1

That you need is to make use of DateTime.ParseExact. The following version:

public static DateTime ParseExact(
    string s,
    string[] formats,
    IFormatProvider provider,
    DateTimeStyles style
)

which

Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.

So, provided that you known exactly the possible formats of the dates, that user will enter you can catch them all with this method. ' For a more detailed explanation of this method, please have a look here .

Christos
  • 53,228
  • 8
  • 76
  • 108