Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:00:42+00:00 2026-06-15T20:00:42+00:00

So I have a string that could be a date of varying formats. I

  • 0

So I have a string that could be a date of varying formats. I wanted to create a method that tries each one until it is successful, then return the converted date, or throw an error if it matches no formats. I wrote this:

private string ConvertDate(string toConvert)
{
    if (string.IsNullOrEmpty(toConvert)) { return ""; }

    DateTime date;
    bool success = DateTime.TryParseExact(toConvert,
            "MMddyy",
            new CultureInfo("en-US"),
            DateTimeStyles.None,
            out date);

    if (!success)
    {
        success = DateTime.TryParseExact(toConvert,
            "MMddyyyy",
                new CultureInfo("en-US"),
            DateTimeStyles.None,
            out date);
    }
    if (!success)
    {
        success = DateTime.TryParseExact(toConvert,
            "MM/dd/yy",
            new CultureInfo("en-US"),
            DateTimeStyles.None,
            out date);
    }
    if (!success)
    {
        success = DateTime.TryParseExact(toConvert,
            "MM/dd/yyyy",
            new CultureInfo("en-US"),
            DateTimeStyles.None,
            out date);
    }
    if (!success) throw new Exception("Date formats are not recognized");

    return date.ToString();
}

It works, but I feel pretty silly writing all that, I figure there must be an easier way to check a lot of different date formats in 1 pass. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:00:43+00:00Added an answer on June 15, 2026 at 8:00 pm

    This is at least easier to maintain. Although the function name is misleading. It’s should probably be something like StandardizeDateFormat.

    private string ConvertDate(string toConvert)
    {
        if (string.IsNullOrEmpty(toConvert)) { return ""; }
    
        string[] dateFormats = new string[]{"MMddyy","MMddyyyy","MM/dd/yy","MM/dd/yyyy"};
        DateTime date;
        bool success = false;
        for(int i = 0;i<dateFormats.Length &&!success;i++)
        {
            success = DateTime.TryParseExact(toConvert,
            dateFormats[i],
                new CultureInfo("en-US"),
                DateTimeStyles.None,
                out date);
        }
        if (!success) throw new Exception("Date formats are not recognized");
    
        return date.ToString();
    }
    

    Edit: Actually, I would probably simplify it even further like so

    private string ConvertDate(string toConvert)
    {
        if (string.IsNullOrEmpty(toConvert)) { return ""; }
    
        string[] dateFormats = new string[]{"MMddyy","MMddyyyy","MM/dd/yy","MM/dd/yyyy"};
        DateTime date;
        for(int i = 0;i<dateFormats.Length ;i++)
        {
            if(DateTime.TryParseExact(toConvert, dateFormats[i], new CultureInfo("en-US"), DateTimeStyles.None, out date))
                return date.ToString();
        }
        throw new Exception("Date formats are not recognized");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single string variable that stores what could be a full date
I'm trying to create a function that could get a date in different formats
For example, I have this string that could change anytime I only want the
I have a string of digits that could vary in length and I want
If I have a string that contains Cat how could I check before hand
i have a string 9 bedrooms in that house, but it could be anything
I have a string like myweb.com/blabla/blabla/image.jpg How could I get substring that starts at
I have a function that builds substrings given a string recursively. Could anyone please
I have a string that contains the representation of a date. It looks like:
I have a string that has a date format of HH:mm so for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.