C# 獲取特殊日期

//1.當前時間
DateTime dt = DateTime.Now; im

//2.本週週一
DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));時間

//3.本週週日
DateTime endWeek = startWeek.AddDays(6);new

//4.本月月初
DateTime startMonth = dt.AddDays(1 - dt.Day); tar

//5.本月月末
DateTime endMonth = startMonth.AddMonths(1).AddDays(-1);
endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1);

//6.本季度初
DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);

//7.本季度末
DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);

//8.本年年初
DateTime startYear = new DateTime(dt.Year, 1, 1);

//9.本年年底
DateTime endYear = new DateTime(dt.Year, 12, 31);

//10.上週一
DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7);

//11.上週末(星期日)
DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7).AddDays(6);

//12.下週一
DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7);

//13.下週末
DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7).AddDays(6);

//14.本年最後一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();

//15.本月初
DateTime.Now.ToString("yyyy-MM-01");

//16.本月最後一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();

//17.上個月第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();

//18.上個月最後一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();

//19.下個月第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();

//20.下下月最後一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();

//21.7天后
DateTime.Now.AddDays(7).ToShortDateString();

//22.7天前
DateTime.Now.AddDays(-7).ToShortDateString();

//23.本年度第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();

//24.本年度最後一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();

//25.上年度第一天,
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();

//26.上年度第最後一天,
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();

//27.下年度第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();

//28.下年度最後一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();

//29.本季度第一天;
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);

//30.本季度的最後一天
DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();

//31.下季度的第一天
DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");

//32.下季度最後一天
DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();

//33.上季度第一天
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);

//34.上季度最後一天DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day).AddDays(-1).ToShortDateString();

相關文章
相關標籤/搜索