I would like to parse a date, identified by month and day, into a valid DateTime
occurring in the future.
Assume today is 2023-05-06
, below is a table of input and output values:
Input | Output |
---|---|
06-24 |
2023-06-24 |
01-05 |
2024-01-05 |
02-29 |
2024-02-29 |
A naive way of achieving this would be to test if using DateTime.Now.Year
for the Year
portion produces a date in the future or not:
var today = DateTime.Now;
bool dateInFuture = new DateTime(today.Year, 02, 29)).CompareTo(today);
However you might have noticed that this will cause an exception as 2023-02-29
is not a valid date.
Ideally I would like this code to be able to work out if the date is actually invalid, so if 00-46
is passed as input it can tell me that that is not a real date, but that's a little tricky as my current implementation fails with dates that are actually valid.
CodePudding user response:
This may help:
var input = "02-29";
var year = DateTime.Today.Year;
if (DateTime.TryParse($"{year}-{input}", out DateTime date))
{
var isFutureDate = date > DateTime.Now;
}
else
{
// TODO: Manage error
}
You can define a method to that:
public static bool TryGetDate(string input, out DateTime date, out bool isFutureDate)
{
var year = DateTime.Today.Year;
if (DateTime.TryParse($"{year}-{input}", out date))
{
isFutureDate = date > DateTime.Now;
return true;
}
isFutureDate = false;
return false;
}
And use it:
var input = "02-29";
if (TryGetDate(input, out DateTime date, out bool isFutureDate))
{
// ...
}
else
{
// TODO: Manage error
}
CodePudding user response:
You could use DateTime.IsLeapYear(year)
- as is shown in this answer in conjuction with DateTime.TryParse for check if the value to convert is a valid DateTime:
You can test this code in dotnetfiddle.net:
DateTime today = DateTime.Now;
Int32 year = DateTime.IsLeapYear(today.Year) ? today.Year : today.AddYears(-2).Year;
DateTime tempDate = DateTime.Now;
if (!DateTime.TryParse(year.ToString() "/02/29", out tempDate))
{
// Date is not valid.
Console.WriteLine("Date (" year.ToString() "/02/29" ") is not valid");
}
else
{
Console.WriteLine("Date is valid: " tempDate.ToString("dd/MM/yyyy"));
}