SQL和C#的常用时间日期处理
C#
//以下的毫秒都采用最大997,而不是999 因为SQL SERVER的精度为3毫秒
//本月的天数
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
//本年的天数 是否是闰年
int daysInYear = DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365;
int daysInYear=new DateTime(DateTime.Now.Year,12,31).DayOfYear;
//本月第一天
DateTime firstDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
//本月的最后一天 本月1号加一个月得下月1号,再剪掉一天就是本月最后一天
DateTime lastDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
//本月最后一天的午夜
DateTime lastDayInMonth2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddMilliseconds(-3);
//本年第一天
DateTime firstDayInYear = new DateTime(DateTime.Now.Year, 1, 1);
//本年最后一天
DateTime lastDayInYear = new DateTime(DateTime.Now.Year, 12, 31);
//本年最后一天的午夜
DateTime lastDayInYear2 = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59, 997);
//得到星期几 星期天为7
int dayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek) < 1 ? 7 : Convert.ToInt32(DateTime.Now.DayOfWeek);
//本周一 Date属性:新的 DateTime,其日期与此实例相同,时间值设置为午夜 12:00:00 (00:00:00)。
DateTime monday =DateTime.Now.Date.AddDays(1 - dayOfWeek);
//本周 星期天
DateTime sunday = DateTime.Now.Date.AddDays(7 - dayOfWeek);
//本周 星期天的午夜
DateTime sunday2 = DateTime.Now.Date.AddDays(8 - dayOfWeek).AddMilliseconds(-3);
//本季度第一天
DateTime firsyDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1);
//本季度最后一天
DateTime lastDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1).AddMonths(3).AddDays(-1);
//本季度最后一天的午夜
DateTime lastDayInQuarter2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1).AddMonths(3).AddMilliseconds(-3);
SQL://本月的天数
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
//本年的天数 是否是闰年
int daysInYear = DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365;
int daysInYear=new DateTime(DateTime.Now.Year,12,31).DayOfYear;
//本月第一天
DateTime firstDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
//本月的最后一天 本月1号加一个月得下月1号,再剪掉一天就是本月最后一天
DateTime lastDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
//本月最后一天的午夜
DateTime lastDayInMonth2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddMilliseconds(-3);
//本年第一天
DateTime firstDayInYear = new DateTime(DateTime.Now.Year, 1, 1);
//本年最后一天
DateTime lastDayInYear = new DateTime(DateTime.Now.Year, 12, 31);
//本年最后一天的午夜
DateTime lastDayInYear2 = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59, 997);
//得到星期几 星期天为7
int dayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek) < 1 ? 7 : Convert.ToInt32(DateTime.Now.DayOfWeek);
//本周一 Date属性:新的 DateTime,其日期与此实例相同,时间值设置为午夜 12:00:00 (00:00:00)。
DateTime monday =DateTime.Now.Date.AddDays(1 - dayOfWeek);
//本周 星期天
DateTime sunday = DateTime.Now.Date.AddDays(7 - dayOfWeek);
//本周 星期天的午夜
DateTime sunday2 = DateTime.Now.Date.AddDays(8 - dayOfWeek).AddMilliseconds(-3);
//本季度第一天
DateTime firsyDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1);
//本季度最后一天
DateTime lastDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1).AddMonths(3).AddDays(-1);
//本季度最后一天的午夜
DateTime lastDayInQuarter2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1).AddMonths(3).AddMilliseconds(-3);
--本月的天数
select day(dateadd(day,-1,dateadd(month,1,convert(char(8),getdate(),120)+'01')))
--本年的天数
select datediff(day,datename(year,getdate())+'-01-01',datename(year,getdate())+'-12-31')+1
--本月第一天
select convert(char(8),getdate(),120)+'01'
--本月最后一天
select dateadd(day,-1,dateadd(month,1,convert(char(8),getdate(),120)+'01'))
--本月最后一天午夜
select dateadd(ms,-3,dateadd(month,1,convert(char(8),getdate(),120)+'01'))
--本年第一天
select convert(char(5),getdate(),120)+'01-01'
--本年最后一天
select convert(char(5),getdate(),120)+'12-31'
--本年最后一天午夜
select convert(char(5),getdate(),120)+'12-31 23:59:59:997'
--抱歉之前的有误,因为系统默认星期天为一周开始,所以先把日期减一天来计算
--本周一
select dateadd(week,datediff(week,0,getdate()-1),0)
--本周 星期天 得到下周一,再减掉一天
select dateadd(day,-1,dateadd(week,datediff(week,0,getdate()-1)+1,0))
--本周星期天的午夜 得到下周一,再减掉3毫秒
select dateadd(ms,-3,dateadd(week,datediff(week,0,getdate()-1)+1,0))
--本季度第一天
select dateadd(quarter,datediff(quarter,0,getdate()),0)
--本季度最后一天
select dateadd(day,-1,dateadd(quarter,datediff(quarter,0,getdate())+1,0))
--本季度最后一天的午夜
select dateadd(ms,-3,dateadd(quarter,datediff(quarter,0,getdate())+1,0))
select day(dateadd(day,-1,dateadd(month,1,convert(char(8),getdate(),120)+'01')))
--本年的天数
select datediff(day,datename(year,getdate())+'-01-01',datename(year,getdate())+'-12-31')+1
--本月第一天
select convert(char(8),getdate(),120)+'01'
--本月最后一天
select dateadd(day,-1,dateadd(month,1,convert(char(8),getdate(),120)+'01'))
--本月最后一天午夜
select dateadd(ms,-3,dateadd(month,1,convert(char(8),getdate(),120)+'01'))
--本年第一天
select convert(char(5),getdate(),120)+'01-01'
--本年最后一天
select convert(char(5),getdate(),120)+'12-31'
--本年最后一天午夜
select convert(char(5),getdate(),120)+'12-31 23:59:59:997'
--抱歉之前的有误,因为系统默认星期天为一周开始,所以先把日期减一天来计算
--本周一
select dateadd(week,datediff(week,0,getdate()-1),0)
--本周 星期天 得到下周一,再减掉一天
select dateadd(day,-1,dateadd(week,datediff(week,0,getdate()-1)+1,0))
--本周星期天的午夜 得到下周一,再减掉3毫秒
select dateadd(ms,-3,dateadd(week,datediff(week,0,getdate()-1)+1,0))
--本季度第一天
select dateadd(quarter,datediff(quarter,0,getdate()),0)
--本季度最后一天
select dateadd(day,-1,dateadd(quarter,datediff(quarter,0,getdate())+1,0))
--本季度最后一天的午夜
select dateadd(ms,-3,dateadd(quarter,datediff(quarter,0,getdate())+1,0))