[ACCEPTED]-TimeSpan using a nullable date-nullable

Accepted answer
Score: 14

To subtract two dates when zero, one or 4 both of them is nullable you just subtract them. The subtraction 3 operator does the right thing; there's no 2 need for you to write all the logic yourself 1 that is already in the subtraction operator.

TimeSpan? timeOnPlan = DateTime.Now - user.PlanStartDate;
return timeOnPlan == null ? 0 : timeOnPlan.Days / 7;
Score: 10

Try this:

weeksOnPlanSpan = DateTime.Now.Subtract(planStartDate.Value); 


Score: 0

Cast the nullable datetime as a normal datetime.

If 2 you know it is not null, then the cast will 1 work fine.

More Related questions