public static DateTime Truncate(this DateTime dateTime, DateTimeSignificance significance) { int year = dateTime.Year; int month = 1, day = 1, hour = 0, minute = 0, second = 0; if (significance != DateTimeSignificance.ToYears) { month = dateTime.Month; if (significance != DateTimeSignificance.ToMonths) { day = dateTime.Day; if (significance != DateTimeSignificance.ToDays) { hour = dateTime.Hour; if (significance != DateTimeSignificance.ToHours) { minute = dateTime.Minute; if (significance != DateTimeSignificance.ToMinutes) { second = dateTime.Second; } } } } } return(new DateTime(year, month, day, hour, minute, second, dateTime.Kind)); }
// ========================================================================== // // Truncation // // ========================================================================== public static DateTime Round(this DateTime dateTime, DateTimeSignificance significance) { DateTime result; int year = dateTime.Year; int month = dateTime.Month; if (significance != DateTimeSignificance.ToYears) { int day = dateTime.Day; if (significance != DateTimeSignificance.ToMonths) { int hour = dateTime.Hour; if (significance != DateTimeSignificance.ToDays) { int minute = dateTime.Minute; if (significance != DateTimeSignificance.ToHours) { int second = dateTime.Second; if (significance != DateTimeSignificance.ToMinutes) { // Seconds result = new DateTime(year, month, day, hour, minute, second, dateTime.Kind); if (dateTime.Millisecond >= 500) { result = result.AddSeconds(1); } } else // minutes { result = new DateTime(year, month, day, hour, minute, 0, dateTime.Kind); if (second >= 30) { result = result.AddMinutes(1); } } } else // hours { result = new DateTime(year, month, day, hour, 0, 0, dateTime.Kind); if (minute >= 30) { result = result.AddHours(1); } } } else // day { result = new DateTime(year, month, day, 0, 0, 0, dateTime.Kind); if (hour >= 12) { result = result.AddDays(1); } } } else // month { result = new DateTime(year, month, 1); int dayThreshold = 16; int daysInMonth = DateTime.DaysInMonth(year, month); if (daysInMonth == 30 || daysInMonth == 29) { dayThreshold = 15; } else if (daysInMonth == 28) { dayThreshold = 14; } if (day > dayThreshold) { result = result.AddMonths(1); } } } else { if (month >= 7) { year++; } result = new DateTime(year, 1, 1, 0, 0, 0, dateTime.Kind); } return(result); }
public static int CompareTo(this DateTime dateTime, DateTime dateToCompare, DateTimeSignificance significance) { int yearsDelta = dateTime.Year - dateToCompare.Year; if (yearsDelta != 0) { return(yearsDelta < 0 ? -1 : 1); } if (significance == DateTimeSignificance.ToYears) { return(0); } int monthsDelta = dateTime.Month - dateToCompare.Month; if (monthsDelta != 0) { return(monthsDelta < 0 ? -1 : 1); } if (significance == DateTimeSignificance.ToMonths) { return(0); } int daysDelta = dateTime.Day - dateToCompare.Day; if (daysDelta != 0) { return(daysDelta < 0 ? -1 : 1); } if (significance == DateTimeSignificance.ToDays) { return(0); } int hoursDelta = dateTime.Hour - dateToCompare.Hour; if (hoursDelta != 0) { return(hoursDelta < 0 ? -1 : 1); } if (significance == DateTimeSignificance.ToHours) { return(0); } int minutesDelta = dateTime.Minute - dateToCompare.Minute; if (minutesDelta != 0) { return(minutesDelta < 0 ? -1 : 1); } if (significance == DateTimeSignificance.ToMinutes) { return(0); } int secondsDelta = dateTime.Second - dateToCompare.Second; if (secondsDelta != 0) { return(secondsDelta < 0 ? -1 : 1); } return(0); }
public static DateTime Truncate(this DateTime dateTime, DateTimeSignificance significance) { int year = dateTime.Year; int month = 1, day = 1, hour = 0, minute = 0, second = 0; if (significance != DateTimeSignificance.ToYears) { month = dateTime.Month; if (significance != DateTimeSignificance.ToMonths) { day = dateTime.Day; if (significance != DateTimeSignificance.ToDays) { hour = dateTime.Hour; if (significance != DateTimeSignificance.ToHours) { minute = dateTime.Minute; if (significance != DateTimeSignificance.ToMinutes) { second = dateTime.Second; } } } } } return new DateTime(year, month, day, hour, minute, second, dateTime.Kind); }
// ========================================================================== // // Truncation // // ========================================================================== public static DateTime Round(this DateTime dateTime, DateTimeSignificance significance) { DateTime result; int year = dateTime.Year; int month = dateTime.Month; if (significance != DateTimeSignificance.ToYears) { int day = dateTime.Day; if (significance != DateTimeSignificance.ToMonths) { int hour = dateTime.Hour; if (significance != DateTimeSignificance.ToDays) { int minute = dateTime.Minute; if (significance != DateTimeSignificance.ToHours) { int second = dateTime.Second; if (significance != DateTimeSignificance.ToMinutes) { // Seconds result = new DateTime(year, month, day, hour, minute, second, dateTime.Kind); if (dateTime.Millisecond >= 500) result = result.AddSeconds(1); } else // minutes { result = new DateTime(year, month, day, hour, minute, 0, dateTime.Kind); if (second >= 30) result = result.AddMinutes(1); } } else // hours { result = new DateTime(year, month, day, hour, 0, 0, dateTime.Kind); if (minute >= 30) result = result.AddHours(1); } } else // day { result = new DateTime(year, month, day, 0, 0, 0, dateTime.Kind); if (hour >= 12) result = result.AddDays(1); } } else // month { result = new DateTime(year, month, 1); int dayThreshold = 16; int daysInMonth = DateTime.DaysInMonth(year, month); if (daysInMonth == 30 || daysInMonth == 29) { dayThreshold = 15; } else if (daysInMonth == 28) { dayThreshold = 14; } if (day > dayThreshold) { result = result.AddMonths(1); } } } else { if (month >= 7) year++; result = new DateTime(year, 1, 1, 0, 0, 0, dateTime.Kind); } return result; }
public static int CompareTo(this DateTime dateTime, DateTime dateToCompare, DateTimeSignificance significance) { int yearsDelta = dateTime.Year - dateToCompare.Year; if (yearsDelta != 0) return yearsDelta < 0 ? -1 : 1; if (significance == DateTimeSignificance.ToYears) return 0; int monthsDelta = dateTime.Month - dateToCompare.Month; if (monthsDelta != 0) return monthsDelta < 0 ? -1 : 1; if (significance == DateTimeSignificance.ToMonths) return 0; int daysDelta = dateTime.Day - dateToCompare.Day; if (daysDelta != 0) return daysDelta < 0 ? -1 : 1; if (significance == DateTimeSignificance.ToDays) return 0; int hoursDelta = dateTime.Hour - dateToCompare.Hour; if (hoursDelta != 0) return hoursDelta < 0 ? -1 : 1; if (significance == DateTimeSignificance.ToHours) return 0; int minutesDelta = dateTime.Minute - dateToCompare.Minute; if (minutesDelta != 0) return minutesDelta < 0 ? -1 : 1; if (significance == DateTimeSignificance.ToMinutes) return 0; int secondsDelta = dateTime.Second - dateToCompare.Second; if (secondsDelta != 0) return secondsDelta < 0 ? -1 : 1; return 0; }