MadDev
MadDev

Reputation: 1150

How to add one year to a date and select a particular month

If the NextInspectionDueDate is less than or equal to today's date then I want to update it's value to the same day but next year and specifically the month of April.

I have tried the below but it gives an 'has some invalid arguments' error. I just can't figure out the correct syntax. Maybe I should be doing it a different way?

if (foundVehicle.Vehicle.NextInspectionDueDate <= DateTime.Now)
{
    foundVehicle.Vehicle.NextInspectionDueDate = new DateTime(DateTime.Now.AddYears(1), 4);
}

Upvotes: 0

Views: 431

Answers (2)

Brutus
Brutus

Reputation: 790

You have just forgotten a parameter for the constructor (the day of the month):

foundVehicle.Vehicle.NextInspectionDueDate = new DateTime(DateTime.Now.AddYears(1).Year, 4, 1);

Upvotes: 3

Jawen
Jawen

Reputation: 1446

You have one missing parameter (day), maybe you can try:

var tempdate = DateTime.Now.AddYears(1); foundVehicle.Vehicle.NextInspectionDueDate = new DateTime(tempdate.Year, 4, tempdate.Day);

If you need to keep current date day

Or

var day = foundVehicle.Vehicle.NextInspectionDueDate.Day; var tempdate = DateTime.Now.AddYears(1); foundVehicle.Vehicle.NextInspectionDueDate = new DateTime(tempdate.Year, 4, day);

If you need to keep original date day

Upvotes: 1

Related Questions