[转]用C#和SQL获得当前月的第一天和最后一天

本文转自:http://abelstudy.spaces.live.com/blog/cns!897200FEE9FDF237!304.entry

C#

int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
DateTime firstDayOfThisMonth = new DateTime(year, month, 1);
DateTime lastDayOfThisMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));

SQL

DECLARE @NOW DATETIME
DECLARE @FistDayOfThisMonth DATETIME
DECLARE @LastDayOfThisMonth DATETIME
SET @NOW = GETDATE()
SET @FistDayOfThisMonth = @NOW - DAY(@NOW) + 1
SET @LastDayOfThisMonth = DATEADD(MONTH, 1, @NOW - DAY(@NOW) + 1) - 1
SELECT @FistDayOfThisMonth
SELECT @LastDayOfThisMonth

posted on 2008-04-29 16:35  freeliver54  阅读(987)  评论(2编辑  收藏  举报

导航