stackoverflow上的回答
DateTime baseDate = DateTime.Today; var today = baseDate; var yesterday = baseDate.AddDays(-1); var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek); var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1); var lastWeekStart = thisWeekStart.AddDays(-7); var lastWeekEnd = thisWeekStart.AddSeconds(-1); var thisMonthStart = baseDate.AddDays(1 - baseDate.Day); var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1); var lastMonthStart = thisMonthStart.AddMonths(-1); var lastMonthEnd = thisMonthStart.AddSeconds(-1);
DevExpress的横轴,根据时间,自适应
https://documentation.devexpress.com/#WindowsForms/CustomDocument6464
周一为每周的第一天,进行时间的推断
private static DayOfWeek firstDayOfWeek = DayOfWeek.Monday; public static DayOfWeek FirstDayOfWeek { get { return firstDayOfWeek;} set { firstDayOfWeek = value; } }
public static DateTime GetWeekStart(this DateTime time) { DayOfWeek lastDayOfWeek; if (firstDayOfWeek == DayOfWeek.Sunday) { lastDayOfWeek = DayOfWeek.Saturday; } else { lastDayOfWeek = firstDayOfWeek - 1; } DateTime weekStart; if (time.DayOfWeek == lastDayOfWeek) { //用默认的第一天减去最后一天,相差6天 weekStart = time.AddDays(DayOfWeek.Sunday - DayOfWeek.Saturday); } else { weekStart = time.AddDays(firstDayOfWeek - time.DayOfWeek); } return weekStart; }