private static DateTime AdjustDate(DateTime date, CalendarOption calendarOption, int value) { switch (calendarOption) { case CalendarOption.SECOND: date.AddSeconds(value); break; case CalendarOption.HOUR: date.AddHours(value); break; case CalendarOption.DAY: date.AddDays(value); break; case CalendarOption.MONTH: date.AddMonths(value); break; case CalendarOption.YEAR: date.AddYears(value); break; } return(date); }
public static long DiffBetweenDates(DateTime startDate, DateTime endDate, CalendarOption calendarOption) { if (endDate > startDate) { return(DiffBetweenDates(endDate, startDate, calendarOption)); } TimeSpan duration = endDate - startDate; switch (calendarOption) { case CalendarOption.SECOND: return(duration.Seconds); case CalendarOption.MINUTE: return(duration.Minutes); case CalendarOption.HOUR: return(duration.Hours); } Assert.That(calendarOption, Is.EqualTo(CalendarOption.SECOND) | Is.EqualTo(CalendarOption.MINUTE) | Is.EqualTo(CalendarOption.HOUR), "Invalid variable used!"); return(0L); }