Answered by:
date - days calculations

Question
-
Answers
-
Hey
Have a look into this link
- Proposed as answer by OsmanGümüş Wednesday, October 12, 2011 2:22 PM
- Marked as answer by Bob Wu-MT Tuesday, October 25, 2011 9:27 AM
-
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. -
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 Bob Wu-MT Tuesday, October 25, 2011 9:27 AM
-
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 Bob Wu-MT Tuesday, October 25, 2011 9:27 AM
All replies
-
Hey
Have a look into this link
- Proposed as answer by OsmanGümüş Wednesday, October 12, 2011 2:22 PM
- Marked as answer by Bob Wu-MT Tuesday, October 25, 2011 9:27 AM
-
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. -
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 Bob Wu-MT Tuesday, October 25, 2011 9:27 AM
-
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 Bob Wu-MT Tuesday, October 25, 2011 9:27 AM