根据日,月,星期几来推年份的算法

 原理说明: 算法:(年度系数+月度系数+日)/7的余数就是星期几
   利用这个算法 倒推已经月,日,星期求年份。
   月度系数1-12:0,3,3,6,1,4,6,2,5,0,3,5
   日:今天是几号,日就是几
   星期:星期一到星期天:1,2,3,4,5,6,

   年度系数推算原理 2007年年度系数0 2008年1-2月是1,3-12月是2 (闰年)2009年3,2010年4,2011年是5,2012年1-2月是6,3-12月是0(闰年)

  一般年份下一年比上一年年度系数+1,遇到润年 1-2月比上一年+1  3-12月比上一年+2 以此类推。

下面是一个简单的例子推测年份的范围订在2013-2020年

function GetYearbydate(month, day, wk:string): string;          //传参 月,日,星期几 返回年份
var
   moth,week,nowyear:integer;
   ms,days,ys:integer;
   i,j:integer;
   year:string;
const
   MothXS:array[0..11]of integer=(0,3,3,6,1,4,6,2,5,0,3,5);    //月度系数
   YearXS:array[0..7]of array[0..1]of integer=((2013,1),(2014,2),(2015,3),(2016,4),(2017,6),(2018,0),(2019,1),(2020,2));//2013-2020年的年度系数 (闰年按1-2月份系)
   YearXSN:array[0..7]of array[0..1]of integer=((2013,1),(2014,2),(2015,3),(2016,5),(2017,6),(2018,0),(2019,1),(2020,3));//2013-2020年的年度系数 (闰年按7-12月的系数)
begin
    nowyear:=strtoint(formatdatetime('yyyy',now));
    moth:=strtoint(month)-1;
    ms:=mothxs[moth];
    days:=strtoint(day);
    week:=strtoint(wk);
    i:=1;
    while i<=10 do
    begin
        ys:=7*i+week-(ms+days);
        if (ys>0)and(ys<6) then
          break;
        inc(i);
    end;
    if (moth+1)>2 then
    begin
        j:=0;
        while j<=7 do
        begin
            if (YearXSN[j][1]=ys)and((YearXSN[j][0]>=nowyear)) then
            begin
                 year:=year+'或者'+inttostr(YearXSN[j][0]);
            end;
            inc(j);
        end;
    end
    else
    begin
        j:=0;
        while j<=7 do
        begin
            if (YearXS[j][1]=ys)and((YearXS[j][0]>=nowyear)) then
            begin
                 year:=year+'或者'+inttostr(YearXS[j][0]);
            end;
            inc(j);
        end;
    end;
    result:=year;
end;

转载于:https://www.cnblogs.com/cjypower/p/3205170.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值