如何计算精确的日期差值`呢?????如:2006-01-19与2006-03-12差几天呢

解决方案 »

  1.   

    加入单元DATEUNTILS,里面有你需要的函数
      

  2.   

    var
        dtBegin,dtEnd:TDateTime;
    begin    dtBegin:=StrToDate('2006-01-19');
        dtEnd:=StrToDate('2006-03-12');
        iDays:=Trunc(dtBegin)-Trunc(dtEnd);
      

  3.   

    在d中,日期类型实际存储为double类型,可以把日期直接转成double类型来计算。
      var
        t1,t2 : tdatetime;
        i : integer;
      begin
        i := trunc(double(t1) - double(t2) );
      end;
      

  4.   

    var
    d1,d2:Tdatetime;
    //数组中0..6分别表示:年、月、日、时、分、秒、毫秒
    a,b:array [0..6]of word;
    begin
    DecodeDate(d1,a[0],a[1],a[2]);
    DecodeTime(d1,a[3],a[4],a[5],a[6]);
    DecodeDate(d2,b[0],b[1],b[2]);
    DecodeTime(d2,b[3],b[4],b[5],b[6]);
    //...接下来就比较 a,b的数值就行了。end;
      

  5.   

    var
      MyDate1,mydate2: TDateTime;
      t:double;
    begin  MyDate1 := EncodeDate(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text));
      Label1.Caption := DateToStr(MyDate1);
      MyDate2 := EncodeDate(StrToInt(Edit4.Text), StrToInt(Edit5.Text), StrToInt(Edit6.Text));
      Label2.Caption := DateToStr(MyDate2);
      t:=mydate2-mydate1;
      label3.Caption:=floattostr(t);
    end;
      

  6.   

    接上
    //Edit1.Text 表示年,Edit2.Text 表示月,Edit3.Text表示日
    //Edit4.Text 表示年,Edit5.Text 表示月,Edit6.Text表示日
      

  7.   

    UnitDateUtilsCategorydatetime routinesDelphi syntax:function DaysBetween(const ANow, AThen: TDateTime): Integer;我就不明白楼上的几个人为何非得那样做呢?
      

  8.   

    Returns the number of days (including fractional days) between two specified TDateTime values.UnitDateUtilsCategorydatetime routinesDelphi syntax:function DaySpan(const ANow, AThen: TDateTime): Double;
    上面的不能用,还有这个,可以返回小数!
      

  9.   

    日期型其实就是double
    (d1-d2)/24/0.041666666
      

  10.   

    trunc(strtodate(2006-03-12)-strtodate(2006-01-19));
      

  11.   

    DayOfTheYear
    功能说明:根据指定日期,获取天数。
    参考实例:
    Label1.Caption := IntToStr(DayOfTheYear(Now));假设当前日期为2005年1月2日,那么Label将显示为2。表示是2005年的第2天。
      

  12.   

    对不起,刚才写错了,应该是这样:
    trunc(strtodate(2006-03-12))-trunc(strtodate(2006-01-19));
      

  13.   

    让我写一个超好用的,哈哈哈var
        dtBegin,dtEnd:TDateTime;
        DayCount: Integer;
    begin    dtBegin  := StrToDate('2006-01-19');
        dtEnd    := StrToDate('2006-03-12');
        DayCount := 0;
        while not SameDate(dtBegin, dtEnd) do begin
            IncDay(dtBegin);
            Inc(DayCount);
        end;
        //return DayCount
    end;