当前位置: 数据库>sqlserver
一些关于c#与Sql的时间的操作
来源: 互联网 发布时间:2014-08-29
本文导语: 一、C# 读取今天所在周、月的开始与结束时间 代码如下: DateTime beginDateTime = DateTime.Now.Date; DateTime endDateTime = beginDateTime; beginDateTime = beginDateTime.AddDays(-(int)beginDateTime.DayOfWeek); //得到周的开始时间 endDateTime = beginDateTime.AddDays(6); ...
一、C# 读取今天所在周、月的开始与结束时间
代码如下:
DateTime beginDateTime = DateTime.Now.Date;
DateTime endDateTime = beginDateTime;
beginDateTime = beginDateTime.AddDays(-(int)beginDateTime.DayOfWeek); //得到周的开始时间
endDateTime = beginDateTime.AddDays(6); //得到周的结束时间
beginDateTime = new DateTime(beginDateTime.Year,beginDateTime.Month,1); //得到月的开始时间
endDateTime = beginDateTime.AddMonths(1).AddDays(-1); //得到月的结束时间
DateTime endDateTime = beginDateTime;
beginDateTime = beginDateTime.AddDays(-(int)beginDateTime.DayOfWeek); //得到周的开始时间
endDateTime = beginDateTime.AddDays(6); //得到周的结束时间
beginDateTime = new DateTime(beginDateTime.Year,beginDateTime.Month,1); //得到月的开始时间
endDateTime = beginDateTime.AddMonths(1).AddDays(-1); //得到月的结束时间
二、sql 读取今天所在的周、月的开始与结束时间
代码如下:
DECLARE @BeginDateTime DateTime
SELECT @BeginDateTime = DATEADD(DAY,1-DATEPART(WEEKDAY,GETDATE()),GETDATE())
SELECT CONVERT(VARCHAR(20),@BeginDateTime,23) AS WeekBeginDateTime,CONVERT(VARCHAR(20),DATEADD(DAY,6,@BeginDateTime),23) AS WeekEndDateTime
SELECT CONVERT(VARCHAR(20),DATEADD(DAY,-DATEPART(DAY,GETDATE())+1,GETDATE()),23) AS MonthBeginDateTime,CONVERT(VARCHAR(20),DATEADD(MONTH,1,DATEADD(DAY,-DATEPART(DAY,GETDATE())+1,GETDATE())),23) AS MonthEndDateTime
SELECT @BeginDateTime = DATEADD(DAY,1-DATEPART(WEEKDAY,GETDATE()),GETDATE())
SELECT CONVERT(VARCHAR(20),@BeginDateTime,23) AS WeekBeginDateTime,CONVERT(VARCHAR(20),DATEADD(DAY,6,@BeginDateTime),23) AS WeekEndDateTime
SELECT CONVERT(VARCHAR(20),DATEADD(DAY,-DATEPART(DAY,GETDATE())+1,GETDATE()),23) AS MonthBeginDateTime,CONVERT(VARCHAR(20),DATEADD(MONTH,1,DATEADD(DAY,-DATEPART(DAY,GETDATE())+1,GETDATE())),23) AS MonthEndDateTime