[C#] C#常用日期时间处理方法 →→→→→进入此内容的聊天室

来自 , 2019-05-01, 写在 C#, 查看 103 次.
URL http://www.code666.cn/view/76908ab3
  1.  //以下的毫秒都采用最大997,而不是999 因为SQL SERVER的精度为3毫秒
  2.             //本月的天数
  3.             int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
  4.  
  5.             //本年的天数 是否是闰年          
  6.             int daysInYear = DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365;
  7.  
  8.             //本月第一天
  9.             DateTime firstDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  10.             //本月的最后一天 本月1号加一个月得下月1号,再剪掉一天就是本月最后一天
  11.             DateTime lastDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
  12.             //本月最后一天的午夜
  13.             DateTime lastDayInMonth2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddMilliseconds(-3);
  14.  
  15.             //本年第一天
  16.             DateTime firstDayInYear = new DateTime(DateTime.Now.Year, 1, 1);
  17.            
  18.             //本年最后一天
  19.             DateTime lastDayInYear = new DateTime(DateTime.Now.Year, 12, 31);
  20.             //本年最后一天的午夜
  21.             DateTime lastDayInYear2 = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59, 997);
  22.  
  23.             //得到星期几 星期天为7
  24.             int dayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek) < 1 ? 7 : Convert.ToInt32(DateTime.Now.DayOfWeek);
  25.             //本周一
  26.             DateTime monday = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day).AddDays(1 - dayOfWeek);
  27.             //本周 星期天
  28.             DateTime sunday = monday.AddDays(6);
  29.             //本周 星期天的午夜
  30.             DateTime sunday2 = monday.AddDays(7).AddMilliseconds(-3);
  31.  
  32.             //本季度第一天
  33.             DateTime firsyDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1);
  34.             //本季度最后一天
  35.             DateTime lastDayInQuarter = firsyDayInQuarter.AddMonths(3).AddDays(-1);
  36.             //本季度最后一天的午夜
  37.             DateTime lastDayInQuarter2 = firsyDayInQuarter.AddMonths(3).AddMilliseconds(-3);
  38.  
  39.  
  40. //csharp/824

回复 "C#常用日期时间处理方法"

这儿你可以回复上面这条便签

captcha