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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:14:23+00:00 2026-06-06T22:14:23+00:00

I am creating date using following code try { newdatetime = new DateTime(2012, 2,

  • 0

I am creating date using following code

 try
 {
    newdatetime = new DateTime(2012, 2, 30);
    break;
 }
 catch (ArgumentOutOfRangeException)
 {
   // Try 29 Feb if not 28.
 }

The catch block is to catch the invalid date like 30 Feb. Is there any way to verify if the date is valid by speciying the parameters like (year, month, day)?

  • 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-06T22:14:24+00:00Added an answer on June 6, 2026 at 10:14 pm

    Well, with months you know the valid range so you can constrain that manually. Years are obviously not constrained in the normal sense, but are instead limited by the amount that DateTime can actually hold (0001 to 9999).

    With days, there is the DaysInMonth(int year, int month) method that can tell you the maximum days for the provided month. This also gives you the leap year.

    With this information, you can create your own method to check the range based on the provided integers.

    Something like:

    public static bool AreValidDateValues(int year, int month, int day)
    {
        if (month < 1 || month > 12)
            return false;
    
        if (year < DateTime.MinValue.Year || year > DateTime.MaxValue.Year)
            return false;
    
        var days = DateTime.DaysInMonth(year, month);
    
        if (day < 1 || day > days)
            return false;
    
        return true;
    }
    

    Or if you can’t be bothered with that, convert the raw values into a string representation of a date and put that into DateTime.TryParse, which will give a true/false for the provided string – just be careful with culture-sensitive parsing.

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

Sidebar

Related Questions

I am trying to get the following: [today's date]___[textfilename].txt from the following code: using
in django I am creating data series using datetime.date() objects, like this: data.append([ddate, daily_score])
I'm working on creating a date/time user control in WPF using C# 2008. My
I created a callback when creating a new Client object instead of using the
I was using the following method for creating NSDate Object based on the timezone
I have a problem creating a database schema for the following scenario: (I’m not
I need to convert the following format into a new format using NSDateFormatter. 'Fri,
Using Internet Explorer (don't appear to have an issue in FireFox) the following code
I'm adding a record to a table using the following code: Dim rs1 As
I'm creating an app that should list the date and time when a button

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.