简体   繁体   中英

How to check date format c#

I have ac# block of code here:

    string inputString = textBox1.Text;
    DateTime dt;
    try
    {
        if (DateTime.TryParseExact(inputString, "yyyyMMdd", null, DateTimeStyles.None, out dt) == true)
        {
            dt = dt.AddMonths(6);
            textBox2.Text = dt.ToString("yyyyMMdd");
        }
        else if (DateTime.TryParseExact(inputString, "yyyy.MM.dd", null, DateTimeStyles.None, out dt) == true)
        {
            dt = dt.AddMonths(6);
            textBox2.Text = dt.ToString("yyyyMMdd");
        }
    }   
    catch (Exception ex) { MessageBox.Show(ex.Message); }

Basically, what it does is, user input a string in textbox1, on button click, C# will check what date format is in the textbox, then add 6 months on the date and output it in the textbox2 to string format yyyyMMdd. As you see as of now, it accepts yyyyMMdd and yyyy.MM.dd and do the same process. But my problem is I still got a lot of date format left:

dd-MM-yy 
yyyy/mm/dd 
yyyy-mm-dd 
yy/mm/dd 

I don't want to use OR in my IF statement. Is there a way like WHATEVER the format is, will be accepted and do the process. Thanks a lot guys!

One of the overloads of TryParseExact accepts as its second parameter an array of strings. It won't tell you exactly which format was used, however. If you really do need that information, then yes, you're just going to have to run the one-format TryParseExact with each format you want, and see which one works.

And, of course, you'll need to make sure that all the formats you allow are unambiguous as to which parts are day, month, and year, or else you might parse 01/02/03 as any of six possible dates.

DateTime.TryParseExact has an overload that accepts multiple formats to try parsing.

DateTime date;
DateTime.TryParseExact("11-Nov-13", new[] { "yyyy-MM-dd","MM/dd/yy","dd-MMM-yy"}, null, DateTimeStyles.None, out date); 

However the comment from Lee Taylor is still correct in that if you have formats that can be ambiguous, you will not know exactly what's wrong.

If your solution requires that you return a formatted string using the same style as provided - then you'll need to pass your formats into a loop and try individually.

You can check for all possible formats via using CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns() which is present under the namespace System.Globalization.

You can use DateTime.TryParseExact which has an overload that accepts multiple formats to try parsing - ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style)

Example Code :

private DateTime GetDate(string date)
{
    DateTime dateTime = new DateTime();
    string[] formats = CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns();

    try
    {
        dateTime = DateTime.ParseExact(date, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
    catch
    {
        throw ...;
    }

    return dateTime;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM