# date - days calculations

• ### Question

• I was wondering if someone could help me with the following problem I have:

given a persons date of hire - calculate the number of days to their next anniversary date from today.

Any help in how to do this would be appreciated.

Wednesday, October 12, 2011 2:10 PM

• Proposed as answer by Wednesday, October 12, 2011 2:22 PM
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 2:19 PM
•

```DateTime dateOfHire;
int hireDay = dateOfHire.DayOfYear;
int today = DateTime.Now.DayOfYear;
if (hireDay < today)
{
//TODO: add days in year based on whether this is a leap year or not
}
int countdown = hireDay - today;
```

I left a bit of edge cases to fill in, but it shouldn't be that hard given that datetime has an IsLeapYear method.

• Edited by Wednesday, October 12, 2011 2:23 PM
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 2:22 PM
• First determine what is the anniversary date this year. Add one year if that date is in the past. Subtract today from that date to obtain a TimeSpan. Get the Days of that TimeSpan.

```DateTime dateOfHire = new DateTime(2000, 11, 1);
DateTime anniversary = new DateTime(DateTime.Today.Year, dateOfHire.Month, dateOfHire.Day);
if (anniversary < DateTime.Today) anniversary = anniversary.AddYears(1);

int countdown = (anniversary - DateTime.Today).Days;
```

• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 3:00 PM
• Use TimeSpan class to get the difference between two dates:

```            DateTime t1 = DateTime.Today;
DateTime t2 = new DateTime(2012, 4, 12);
TimeSpan ts = t2.Subtract(t1);
int daysDiff = (int)ts.TotalDays;
```

Mitja
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 3:19 PM

### All replies

• Proposed as answer by Wednesday, October 12, 2011 2:22 PM
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 2:19 PM
•

```DateTime dateOfHire;
int hireDay = dateOfHire.DayOfYear;
int today = DateTime.Now.DayOfYear;
if (hireDay < today)
{
//TODO: add days in year based on whether this is a leap year or not
}
int countdown = hireDay - today;
```

I left a bit of edge cases to fill in, but it shouldn't be that hard given that datetime has an IsLeapYear method.

• Edited by Wednesday, October 12, 2011 2:23 PM
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 2:22 PM
• First determine what is the anniversary date this year. Add one year if that date is in the past. Subtract today from that date to obtain a TimeSpan. Get the Days of that TimeSpan.

```DateTime dateOfHire = new DateTime(2000, 11, 1);
DateTime anniversary = new DateTime(DateTime.Today.Year, dateOfHire.Month, dateOfHire.Day);
if (anniversary < DateTime.Today) anniversary = anniversary.AddYears(1);

int countdown = (anniversary - DateTime.Today).Days;
```

• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 3:00 PM
• Use TimeSpan class to get the difference between two dates:

```            DateTime t1 = DateTime.Today;
DateTime t2 = new DateTime(2012, 4, 12);
TimeSpan ts = t2.Subtract(t1);
int daysDiff = (int)ts.TotalDays;
```

Mitja
• Marked as answer by Tuesday, October 25, 2011 9:27 AM
Wednesday, October 12, 2011 3:19 PM