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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:52:09+00:00 2026-05-30T11:52:09+00:00

The title basically says it all. I’m getting three user-supplied integers ( year ,

  • 0

The title basically says it all. I’m getting three user-supplied integers (year, month, day)1 from a legacy database (which I cannot change). Currently, I use the following code to parse those integers into a DateTime structure:

try {
    return new DateTime(year, month, day);
} catch (ArgumentException ex) {
    return DateTime.MinValue;
}

Sometimes, the values don’t represent a valid date (yes, users enter stuff like 1999-06-31, and no, the legacy app did not verify this). Since throwing an exception when data validation fails is considered bad practice, I’d prefer to replace this with exception-less code. However, the only solution I could find was to convert the integers into one string and TryParseExact this string, which seems even uglier to me. Did I miss some obvious better solution?


1 Actually, it’s one integer in the format YYYYMMDD, but converting that to year, month and day is trivial…

  • 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-30T11:52:11+00:00Added an answer on May 30, 2026 at 11:52 am

    There is not a static function IsValidDate() so you have to write it by yourself, first naive implementation may be:

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

    I said this is a naive implementation because (besides arguments range) the only check to see if a date exists is for leap year. In practice this may fail because of calendar issues if you’re working with non Gregorian calendars (and missing days even in Gregorian calendar that has been used to align date from Julian calendar).

    Working With Calendars

    These assumptions may be broken for non Gregorian calendars:

    • 1 January 01 is smallest valid date. It’s not true. Different calendars have a different smallest date. This limit is just DateTime technical limit but there may be a calendar (or an Era within a calendar) with a different minimum (and maximum) date.
    • Number of months in one year is less or equal than 12. It’s not true, in some calendars upper bound is 13 and it’s not always the same for every year.
    • If a date is valid (according all other rules) then it’s a valid date. It’s not true, a calendar may have more than one era and not all dates are valid (possibly even within era date range).

    Rules to manage this are pretty complex and it’s too easy to forget something so, in this case, catching an exception may not be such bad idea. A better version of previous validation function may just provide basic validation and relying on DateTime to check other rules:

    public static DateTime? TryNew(int year,
                                   int month,
                                   int day,
                                   Calendar calendar)
    {
        if (calendar == null)
            calendar = new GregorianCalendar();
    
        if (year < calendar.MinSupportedDateTime.Year)
            return null;
    
        if (year > calendar.MaxSupportedDateTime.Year)
            return null;
    
        // Note that even with this check we can't assert this is a valid
        // month because one year may be "shared" for two eras moreover here
        // we're assuming current era.
        if (month < 1 || month > calendar.GetMonthsInYear(year))
            return null;
    
        if (day <= 0 || day > DateTime.DaysInMonth(year, month))
            return null;
    
        // Now, probably, date is valid but there may still be issues
        // about era and missing days because of calendar changes.
        // For all this checks we rely on DateTime implementation.        
        try
        {
            return new DateTime(year, month, day, calendar);
        }
        catch (ArgumentOutOfRangeException)
        {
            return null;
        }
    }
    

    Then, given this new function, your original code should be:

    return TryNew(year, month, day) ?? DateTime.MinValue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the confusing title, but it basically says it all. Here's the structures
The title basically says it all. I have a hexadecimal string representing a private
Title basically says it all. I've tried Googling but return a load of false
The title basically says it all. In my case, I have a polyline and
the title basically says it, i have a from with a treeview and a
The question title basically says it all. Is it possible in JPA/Hibernate to gracefully
The title basically says it all. I mainly want to do this so that
The title basically says it all. I'm aware this can't be done using traditional
The title description basically says it all. I'd like to use something that requires
The title basically says it all. Suppose I have an element which I want

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.