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

  • SEARCH
  • Home
  • 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 499961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:02:19+00:00 2026-05-13T06:02:19+00:00

I had posted a question on DateTime to String conversion, I got many satisfying

  • 0

I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much ..
Here is one more problem of String manupulation, I am stuck with ..

I have to convert a string (from some external source) using C# code .. the string can have these expected format of DateTime ..

  1. 02/31/2009 01:59:59           24 hours format
  2. 02/31/2009 01:59:59 AM     12 hours format
  3. 2/31/2009 1:59:59
  4. 2/31/2009 1:59:59 AM
  5. 02/01/2009 01:59:59 AM
  6. 2/1/2009 1:59:59
  7. and so on …….

I tried using DateTime(Convert.ToInt32(string_date.Substring(6,4)),Int,Int,Int,Int,Int,Int)
ie, By extracting the values of month, Day etc

But it doesn’t work .. because I can’t extract the values with substring perfectly .. as the length of string is Varying
I also have tried to extract the values referring the occurance of “/”, “space” and “:” but it becomes bottle neck to derive with (non-)Occurrence of AM/PM

Only the length of Day, Month and Hours can vary ..

  • 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-05-13T06:02:19+00:00Added an answer on May 13, 2026 at 6:02 am

    You can use the DateTime.ParseExact overload that takes a list of formats:

    private static string[] formats = new string[]
        {
            "MM/dd/yyyy HH:mm:ss tt",
            "MM/dd/yyyy HH:mm:ss",
            "M/dd/yyyy H:mm:ss tt",
            "M/dd/yyyy H:mm:ss"        
        };
    
    private static DateTime ParseDate(string input)
    {
        return DateTime.ParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
    

    This will throw a FormatException if the passed string does not match any of the given formats. Notice that the formats expecting AM/PM should appear before identical formats without AM/PM ("MM/dd/yyyy HH:mm:ss tt" comes before "MM/dd/yyyy HH:mm:ss").

    Update
    As Henk points out in the comments, the same functionality is available when using TryParseExact which removes exception situation. Also, paired with nullable types this can be made a bit cleaner:

    private static DateTime? ParseDate(string input)
    {
        DateTime result;
        if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
        {
            return result;
        }
        return null;
    }
    

    Now it will simply return a null reference if it fails to parse the input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had posted a question in regards to this code. I found that JTextArea
(This is technically an addendum to an earlier StackOverflow question I had posted, but
Hi I posted a question a little earlier and I had some replies that
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
I had posted another question earlier about deployment with Passenger. That problem turned out
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
I posted another one on this,, but i had to edit it really much...
I had posted this question on superuser but didn't get a helpful response. Thought
I had posted this question on MSDN Forum http://social.msdn.microsoft.com/Forums/en/vsreportcontrols/thread/f00e3406-354d-4f54-acce-7b7f0ad4c90f But I am not getting
I had posted the question once to http://doctype.com/within-same-line-have-center-element-right-most-element , but didn't get a good

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.