c#根据当前时间获取本周,本月,本年度等时间段和DateTime日期格式化

2023-07-29,,

   DateTime dt = DateTime.Now;  //当前时间  

   DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
4   DateTime endWeek = startWeek.AddDays(); //本周周日   DateTime startMonth = dt.AddDays( - dt.Day); //本月月初
  DateTime endMonth = startMonth.AddMonths().AddDays(-); //本月月末 9   DateTime startQuarter = dt.AddMonths( - (dt.Month - ) % ).AddDays( - dt.Day); //本季度初
  DateTime endQuarter = startQuarter.AddMonths().AddDays(-); //本季度末 12   DateTime startYear = new DateTime(dt.Year, , ); //本年年初
  DateTime endYear = new DateTime(dt.Year, , ); //本年年末

  DateTime日期格式化

   DateTime.Now.ToString(); // 2016/5/9 13:09:55 短日期 长时间
  DateTime.Now.ToString("d"); // 2016/5/9 短日期
  DateTime.Now.ToString("D"); // 2016年5月9日 长日期
  DateTime.Now.ToString("f"); // 2016年5月9日 13:09 短时间 长日期
  DateTime.Now.ToString("F"); // 2016年5月9日 13:09:55 长日期 长时间
  DateTime.Now.ToString("g"); // 2016/5/9 13:09 短日期 短时间
  DateTime.Now.ToString("G"); // 2016/5/9 13:09:55 短日期 长时间
  DateTime.Now.ToString("t"); // 13:09 短时间
  DateTime.Now.ToString("T"); // 13:09:55 长时间
10   DateTime.Now.ToString("m"); // 5月9日
  DateTime.Now.ToString("M"); // 5月9日
  DateTime.Now.ToString("r"); // Mon, 09 May 2016 13:09:55 GMT
  DateTime.Now.ToString("R"); // Mon, 09 May 2016 13:09:55 GMT
  DateTime.Now.ToString("y"); // 2016年5月
  DateTime.Now.ToString("Y"); // 2016年5月

c#根据当前时间获取本周,本月,本年度时间段和DateTime日期格式化的相关教程结束。

《c#根据当前时间获取本周,本月,本年度等时间段和DateTime日期格式化.doc》

下载本文的Word格式文档,以方便收藏与打印。