-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?

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you provide a code? I dont now how can I put multiple formats at the DateTime.ParseExact() method?
Have you had a look to the content of link I've posted? There is an example at the end of the article.
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 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: msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
Can I have DateTimeStyles.AssumeUniversal ?
|
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.

1 Comment

But I dont now what type of input I will have (American or United Kingdom locales)
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));

Comments

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.