enzom83
enzom83

Reputation: 8310

Checks whether a DateTime belongs to the next day with respect to a reference DateTime

Given a reference DateTime and given a DateTime to be verified, how can I verify that the second date belongs to the next day (from midnight onwards)?

private bool IsTheNextDay(DateTime toBeVerified, DateTime referenceDate)
{
    DateTime date = new DateTime(referenceDate.Year, referenceDate.Month, referenceDate.Day);
    DateTime next = date.AddDays(1);
    return (toBeVerified >= next);
}

Using the above source code, it works. Are there other better solutions?

Upvotes: 2

Views: 1910

Answers (1)

Oded
Oded

Reputation: 498942

Here is one way:

private bool IsTheNextDay(DateTime toBeVerified, DateTime referenceDate)
{
    return referenceDate.Date.AddDays(1) == toBeVerified.Date;
}

The Date property simply uses 0 for the hourse/minutes/seconds/milliseconds components.

Upvotes: 6

Related Questions