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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:22:22+00:00 2026-05-16T16:22:22+00:00

I have run into a datetime related problem, my program needs to varify whether

  • 0

I have run into a datetime related problem, my program needs to varify whether a user entered date string is valid.

The program is designed to process date value ranges from 01/01/2000 ~ 31/12/2020 and the string format is like “12 Feb 10”.

The problem I am facing is, sometimes user enters value like “30 Feb 10” (this value is invalid), which passes format check but DateTime.TryParse will interprate this string as “10/02/1930 12:00:00am”.

My solution to this problem is to extract the date, month, year value from the string seperatly and try to recontruct the date string. see the following code.

private static void IsValidDateValue(string userInputValue, CustomValidatorExtended custValidator, string errorMessage, ref bool isValid)
    {
        Regex regexValue = new Regex(SHORT_DATE_VALUE);
        if (regexValue.IsMatch(userInputValue))
        {
            Match match = regexValue.Match(userInputValue);
            int dayValue;
            if (!Int32.TryParse(match.Groups["date"].Value, out dayValue))
            {
                custValidator.ErrorMessage = errorMessage;
                isValid = false;
                return;
            }
            int monthValue;
            if (!Int32.TryParse(ConvertMonthNameToNumber(match.Groups["month"].Value).ToString(), out monthValue))
            {
                custValidator.ErrorMessage = errorMessage;
                isValid = false;
                return;
            }
            //this application is designed to handle only from year 2000 ~ 2020 and user only suppose to enter 2 digits for year 
            int yearValue;
            if (!Int32.TryParse("20" + match.Groups["year"].Value, out yearValue))
            {
                custValidator.ErrorMessage = errorMessage;
                isValid = false;
                return;
            }
            DateTime dtParse;
            if (!DateTime.TryParse(yearValue + "-" + monthValue + "-" + dayValue, out dtParse))
            {
                custValidator.ErrorMessage = errorMessage;
                isValid = false;
                return;
            }
        }
        else
        {
            isValid = true;
            return;
        }
    }

Is there an easier way using .net framework default method to solve this date year value swap problem ?

Thank & Regards,

  • 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-16T16:22:23+00:00Added an answer on May 16, 2026 at 4:22 pm
    DateTime dt = default(DateTime);
    string val = "31 Feb 09";
    bool valid = DateTime.TryParseExact(val, "d MMM yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
    

    Sets valid to false, and leaves dt unchanged. With val set to "31 Mar 09" it sets valid to true and dt contains 2009-03-31T00:00:00 with unspecified timezone. Use different DateTimeStyles values to specify local or UTC if needed.

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

Sidebar

Related Questions

I have run into a problem with Eclipse Indigo, attempting to start up my
I have run into a little problem. I am connecting to a webservice that
I have run into a problem trying to modify a form I myself have
I have run into a bit of a problem here: I had a problem-specific
I have run into a problem w/ my model for databinding in WPF. I
I have run into a problem... I'm trying to use QTKit in an application
I'm using Thauber's lovely Django schedule app , but have run into a problem:
I'm new to MVC3 and have run into a little problem. I've scoured the
I have run into this problem a few times and have never come up
I have run into a couple of similar quirks regarding uint usage in both

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.