比如日期变量是:2007-10-27如何判断,其是否是在10-3到12-11之间呢?用那个函数?

解决方案 »

  1.   

    function CompareDateTime(const A, B: TDateTime): TValueRelationship;function SameDateTime(const A, B: TDateTime): Boolean;function CompareDate(const A, B: TDateTime): TValueRelationship;function SameDate(const A, B: TDateTime): Boolean;function CompareTime(const A, B: TDateTime): TValueRelationship;function SameTime(const A, B: TDateTime): Boolean;
     
      
     
      

  2.   

    转use DateUtils;(此单元里有许多关于时间的函数)。我今天就只看了这么多了,还有许多读自己去看吧。
    里面有太多的关于时间的函数了,如:输入一个时间,可以提取date或time部分,此月有多少天,比较两个时间的’大小’,…..自己看帮助。
    function CompareDate(const A, B: TDateTime): TValueRelationship;比较A,B日期哪个时间先发生?还是同时发生?
    function CompareDateTime(const A, B: TDateTime): TValueRelationship
    function DaysBetween(const ANow, AThen: TDateTime): Integer;两时间相隔多少天?
    function DaysInAMonth(const AYear, AMonth: Word): Word;某年某月有多少天
    function DaysInAYear(const AYear: Word): Word;某年有多少天。
    function DaysInMonth(const AValue: TDateTime): Word;某月有多少天
    procedure DecodeDateMonthWeek(const AValue: TDateTime; out AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);对应out参数:哪年,哪月,此月的第几周,是周几。
    function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;输入某年,此年第几天,out得到此天日期。
    DayOfWeek(TDateTime)某个时间是星期几?
    DayOfTheYear(TDateTime)一年的第几天。
    function DayOfTheMonth(const AValue: TDateTime): Word;第几天of  the Month.
    function DateOf(const AValue: TDateTime): TDateTime;提取时间部分
    这个方法可以完成你的要求。用法可见帮助示例。
    procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay,
                           AHour, AMinute, ASecond, AMilliSecond: Word); (注它必须引用DateUtils单元);
    它把时间的各个部分都分离出来了。与之对应的函数为: 
          function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word):TDateTime;
          function  IncMinute (const AValue: TDateTime; const ANumberOfMinute: Int64 = 1): TDateTime;
                    例:IncMinute(now,N);增加N分钟在NOW的基础上。可以为负。
        
      

  3.   

    var
       date1,date2,date3:TDate;
    begin
       date1:=StrToDate('2007-10-27');
       date2:=StrToDate('2007-10-3');
       date3:=StrToDate('2007-12-11');
       if (date1>date2)and(date1<date3) then
       begin
           date1在date2与date3之间
       end;
    end;TDate、TDateTime类型就是一个Double,直接用大于、小于、等于等比较就可以
      

  4.   


    我要的是:
    不管那个LogDate是那一年的,只要它的月和日在那个日期间就出...
    问题是:有跨年的问题?不行,你录入一个2008-1-20看看,或是2008-12-22..有时行,有时不行...---------------------------------------------------------- LogDate=StrToDate(datetoStr(DateTimePicker1.Date)); 
     thisyear=Yearof(DateTimePicker1.Date); 
        
    ...
        if (LogDate>= StrToDate(IntToStr(thisyear-1)+'-12-22')) and (LogDate<=StrToDate(IntToStr(thisyear-1)+'-1-20'))  then  //出错跨年问题
            Status='记录过期';
    ...
      

  5.   

    var
      logdate: TDateTime;
      vYear: Word;
    begin
      LogDate := StrToDate(datetoStr(DateTimePicker1.Date));
      vYear := Yearof(DateTimePicker1.Date);
      if   (LogDate>= StrToDate(IntToStr(vYear)+'-'+trim(edit1.Text)))
         and   (LogDate <=StrToDate(IntToStr(vYear)+'-'+trim(edit2.Text)))     then
      begin
        showmessage('日期在其间');
      end else
      begin
        showmessage('不在其实间');
      end;
    end;
      

  6.   

    对于日期判断,如果要达到楼主的目的,需要以StrToDate这类函数来重组一个日期,这个时候要注意日期的年月日分隔符。因为你从DataTimePicker中取到的日期格式可能不是以 - 来分隔的,但在下面重组日期的过程中又用的是 - 符号.
      

  7.   

    很简单,使用excodeDate(y,m,d)函数得到m,d,然后用encodeDate(y,m,d)重组一个Tdatetime.
    最后 if Date1 > date2 then ......以上所有函数不涉及字符串操作,执行效率高。strtoDate等函数实现起来比较笨拙
      

  8.   

    直接用< 或者 >比较符就可以了,系统会自动考虑年的问题,你可以测试下给日期加1的情况
      

  9.   

    自定义函数:
    function blInDate(dtSource: TDate; strDTStart,
      strDTEnd: string): boolean;
    begin  if  (StrToDate((FormatDateTime('yyyy',dtSource) + '-' + strDTStart)) <= dtSource ) and
          (StrToDate((FormatDateTime('yyyy',dtSource) + '-' + strDTEnd))   >= dtSource )   then
      begin
        result := true;
      end else
      begin
        result := false;
      end;
    end;调用方法:
    procedure TForm1.Button1Click(Sender: TObject);
    var
     dt : Tdate;
    begin
     dt := StrToDate('2007-10-27');
     if blInDate(dt,'10-3','12-11') then
      begin
        showmessage('成功');
      end else
      begin
        showmessage('失败');
      end;
    end;