using System; namespace API.Extensions; public static class DateTimeExtensions { /// /// Truncates a DateTime to a specified resolution. /// A convenient source for resolution is TimeSpan.TicksPerXXXX constants. /// /// The DateTime object to truncate /// e.g. to round to nearest second, TimeSpan.TicksPerSecond /// Truncated DateTime public static DateTime Truncate(this DateTime date, long resolution) { return new DateTime(date.Ticks - (date.Ticks % resolution), date.Kind); } public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7; return dt.AddDays(-1 * diff).Date; } }