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 6581351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:08:30+00:00 2026-05-25T16:08:30+00:00

I have a single string variable that stores what could be a full date

  • 0

I have a single string variable that stores what could be a full date or a partial date:

1) Full Date: 12/12/2010 12:33 AM

2) Partial Date: 12:33 AM (no date field only time)

I’m trying to figure out what would be the best approach to parse the string to figure out if the string is missing the date string or not. The reason is, in my code if the date is missing I will append a default date to the string (such as 1/1/1900). Keep in mind that the time could be in various formats.

Update – My particular answer to this problem.

As all the “posts” have stated, there are multiple answers to this problem, this is ultimately what I used and hope it can help others*:

public DateTime ProcessDateAndTime(string dateString)
{
    string dateAndTimeString = dateString;

    string[] timeFormats = new string[]
    {
        "hh:mm tt", "hh:mm:ss tt",
        "h:mm tt", "h:mm:ss tt", 
        "HH:mm:ss", "HH:mm", "H:mm"
    };
    // check to see if the date string has a time only
    DateTime dateTimeTemp;
    if (DateTime.TryParseExact(dateString, timeFormats,
        CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out dateTimeTemp))
    {
        // setting date to 01/01/1900
        dateAndTimeString = new DateTime(1900, 1, 1).ToShortDateString() + " " + dateString;
    }

    return DateTime.Parse(dateAndTimeString);
}

*Note: This method is based on the assumption that there are only a specific amount of time formats used in your application, and that it is guaranteed that a properly formatted date and time, or time only string passed in (pre-validation for removal of garbage text).

  • 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-25T16:08:31+00:00Added an answer on May 25, 2026 at 4:08 pm

    You can use DateTime.TryParseExact.

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

Sidebar

Related Questions

I have a variable $v that can be either single string or array of
I have a single AMQ queue that receives simple messages with string body. Consider
So I have a view controller with a single UITableView that shows a string
I have a variable containing a string and in run time i was to
I have a string from which I wish to extract a single word, but
I have a string like this BRADI5G20430|BRADI5G20430.1||1 How can I replace the bar (single
I have the following connection string, and you will notice Provider's.Tests, notice the single
I have an single threaded, embedded application that allocates and deallocates lots and lots
I have a single user java program that I would like to have store
I have a single line CEikLabel in my application that needs to scroll text.

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.