DateTime dt = DateTime.Now
DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")))
DateTime endWeek = startWeek.AddDays(6)
DateTime startMonth = dt.AddDays(1 - dt.Day)
DateTime endMonth = startMonth.AddMonths(1).AddDays(-1)
//DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1)
DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day)
DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1)
DateTime startYear = new DateTime(dt.Year, 1, 1)
DateTime endYear = new DateTime(dt.Year, 12, 31)
、 、 、 、 、 , AddDays()、AddMonths()、AddYears() 。
C# datetime
// ,
// DayOfWeek , , switch ,
string[] Day = new string[] { " ", " ", " ", " ", " ", " ", " " }
string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString()
// , , 7 , 7 ,
DateTime.Now.AddDays(Convert.ToInt32 (1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7)
DateTime.Now.AddDays(Convert.ToInt32 (1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7).AddDays(6)
//
DateTime.Now.AddDays(Convert.ToInt32 (1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7)
DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7).AddDays(6)
// , 1 , 。
//
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString()
// C# ToString
DateTime.Now.ToString("yyyy-MM-01")
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString()
// ,
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString()
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString()
// ,
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString()
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString()
//7
DateTime.Now.Date.ToShortDateString()
DateTime.Now.AddDays(7).ToShortDateString()
//7
DateTime.Now.AddDays(-7).ToShortDateString()
DateTime.Now.Date.ToShortDateString()
// , ToString
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString()
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString()
// ,
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString()
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString()
//
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString()
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString()
// , , 。 , ,
// ,
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day)
// ,
DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString()
// , 。。。。
DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")
DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString()
//
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)). AddDays(1 - DateTime.Now)
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day).AddDays(-1).ToShortDateString()