• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

计算日期段内有几个星期几(SQL,Delphi),日期段内有几个星期天

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

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;



 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Carsim与指定Matlab版本进行联合仿真发布时间:2022-07-18
下一篇:
Scilab语法与Matlab的主要区别【z】发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap