declare @StartDate datetime declare @EndDate datetime declare @WeekDay INT, @WeekNum INT
set @StartDate = '2009-10-4' set @EndDate = '2009-10-25'
set @StartDate = '2009-10-1' set @EndDate = '2009-10-2'
set @StartDate = '2009-10-3' set @EndDate = '2009-10-4'
SET @WeekDay = 1 --星期天
SELECT @WeekNum=datediff(day ,case when DATEPART(weekday,@StartDate) = @WeekDay then @StartDate - 1 else @StartDate end ,case when DATEPART(weekday,@EndDate) = @WeekDay then @EndDate + 1 else @EndDate end ) / 7
IF DATEPART(weekday,@StartDate)=@WeekDay SELECT @WeekNum = @WeekNum + 1 ELSE IF (@EndDate > @StartDate) AND (DATEPART(weekday,@EndDate) = @WeekDay) SELECT @WeekNum = @WeekNum + 1
SELECT @WeekNum
-----Delphi
function WeekDayCount(pvDate1, pvDate2: TDateTime; pvWeekDay: Integer): Integer; var lvDate1, lvDate2: TDateTime; lvWeekNum: Real; lvDay: Integer; begin lvDate1 := Trunc(pvDate1); lvDate2 := Trunc(pvDate2);
if DayOfWeek(pvDate1) = pvWeekDay then lvDate1 := IncDay(lvDate1, -1); if DayOfWeek(lvDate2) = pvWeekDay then lvDate2 := IncDay(lvDate2, +1);
//相差的天数是是多少个星期 Result := Trunc(WeekSpan(lvDate1, lvDate2));
//如果最后一天是那天则加1,注意是pvDate2不是lvDate2 if DayOfWeek(pvDate1) = pvWeekDay then Inc(Result) else if pvDate2 > pvDate1 then if DayOfWeek(pvDate2) = pvWeekDay then Inc(Result); end;
type
TWeekDay = 1..7;
TWeekDays = set of TWeekDay;
function WeekDayCountEx(pvDate1, pvDate2: TDateTime; pvWeekDays: TWeekDays): Integer; var lvDate1, lvDate2, lvTempDate: TDateTime; begin lvDate1 := Trunc(pvDate1); lvDate2 := Trunc(pvDate2); lvTempDate := lvDate1; Result := 0; while lvTempDate <= lvDate2 do begin if DayOfWeek(lvTempDate) in pvWeekDays then Inc(Result); lvTempDate := IncDay(lvTempDate, 1); end; end;
|
请发表评论