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;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…