从系统获取日期时间,NOW()。假如做如下规定:当日08:30 到 晚上 20:30属于A区间,晚上20:30到次日早晨8:30属于B区间。
如何组合该字符串:  日期(月 日)+A/B如果是2006.3.15 08:50:00 那就是 ‘315A’
如果是2006.3.15 22:00:00 那就是 ’315B‘ ,同样2006.3.16 03:00:00就是’315B‘。
第2天的8:30以前也应该属于前一天的B区间。这个算法如何写比较简洁呢?请教各位。谢谢!

解决方案 »

  1.   

    给你一个思路吧,将Now减去8:30,将时间部分和0.5比较,大于就是B,小于就是A然后将日期部分的月和日取出来,三个组合应该没有难点,还有疑问请提出。
      

  2.   

    function TForm1.GetDateStatus(CurDateTime: TDatetime): string;
    begin
      Case StrtoInt(FormatDateTime('hhnn',CurDateTime)) of
        0000..0830:Result:=FormatDateTime('mmdd',CurDateTime)+'B';
        0831..2030:Result:=FormatDateTime('mmdd',CurDateTime)+'A';
        2031..2359:Result:=FormatDateTime('mmdd',CurDateTime)+'B';
      end;
    end;
      

  3.   

    哈哈,表達式歧義了,時間日期,就不要用字符串嘛,直接TDateTime,浮点数操作。
    當前時間Now()減去自己的整數部分(会取整吧),結果如果>=(8.5/24)而且<=(20.5/24)就是A區間,否則是B區間,这个簡單吧。
      

  4.   

    function Getxxx: string;
    var
      Time, Tile: double;
    begin
      Time := Now;
      Tile := Frac(Time);  Result := FormatDateTime('MDD', time) + chr($41 + ord((Tile<8.5/24) or (Tile>20.5/24)));
      //注8.5为8:30, 20.5为20:30
    end;
      

  5.   

    感谢网友:leeky(雅痞·千年虫)的建议,算法比较明朗。var
      Year, Month, Day, Hour, Min, Sec, MSec:Word;
      syear,smonth,sday,shour,smin,ssec,sdate:string;
      Present:TDateTime;
      checkdt,checkother:boolean;begin
      present:=now()-date();  
    //这样就能直接取整了吧?!  checkdt:=(present>(8.5/24))and(present<(20.5/24)); 
    //判断区间,在8:00到20:30内就是true,否则就是false
      checkother:=(present>(20.5/24))and(present<(1));
    //判断是否在20:30到23:59内,如果是就true,否则false  if checkdt=true then
           begin
             present:=now();
             DecodeDate(Present, Year, Month, Day);
             DecodeTime(Present, Hour, Min, Sec, MSec);
             syear:=inttostr(year);
             smonth:=inttostr(Month);
             sday:=inttostr(Day);  //分解年、月、日的字符串         syear:=copy(syear,3,2);
             if length(smonth)<2 then
             smonth:='0'+smonth;
             if length(sday)<2 then
             sday:='0'+sday;        //个位数的补0         sdate:=smonth+sday+'D';   
             showmessage(sdate);     //组合显示
           end
       else
           begin
              if checkother=true then Present:=now()   //是20:30到23:59还是算该天的B
              else Present:=now()-1;       //第2天的早晨00:00到08:30还是前一天的B          DecodeDate(Present, Year, Month, Day);
              DecodeTime(Present, Hour, Min, Sec, MSec);
              syear:=inttostr(year);
              smonth:=inttostr(Month);
              sday:=inttostr(Day);          syear:=copy(syear,3,2);
              if length(smonth)<2 then
              smonth:='0'+smonth;
              if length(sday)<2 then
              sday:='0'+sday;
              sdate:=smonth+sday+'N';
              showmessage(sdate);
           end;end;
      

  6.   

    ShowMessage(IntToStr(MonthOf(now - StrToTime('8:30'))) + IntToStr(DayOf(now - StrToTime('8:30'))) + chr($41 + ord(StrToTime(IntToStr(HourOf(now - StrToTime('8:30')))+':'+IntToStr(MinuteOf(now - StrToTime('8:30'))))<0.5)));
      

  7.   

    搞这么复杂的程序做什么呢?ShowMessage(FormatDateTime('MMDD', (now - StrToTime('8:30'))) + chr($41 + ord(StrToTime(IntToStr(HourOf(now - StrToTime('8:30')))+':'+IntToStr(MinuteOf(now - StrToTime('8:30'))))<0.5)));这样就可以了
      

  8.   

    楼上的不对。2006.3.15 23:20:22应该是0315B吧?你的还是0315A。2006.3.16 05:00:00应该是0315B吧?你的还是0315A。
      

  9.   

    其实有用的就是下面几句:  present:=now()-date();  
      checkdt:=(present>(8.5/24))and(present<(20.5/24)); 
      checkother:=(present>(20.5/24))and(present<(1));