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

 原理说明: 算法:(年度系数+月度系数+日)/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;

posted on 2013-07-22 13:42  七夜雪狐  阅读(797)  评论(0编辑  收藏  举报