从系统获取日期时间,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区间。这个算法如何写比较简洁呢?请教各位。谢谢!
如何组合该字符串: 日期(月 日)+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区间。这个算法如何写比较简洁呢?请教各位。谢谢!
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;
當前時間Now()減去自己的整數部分(会取整吧),結果如果>=(8.5/24)而且<=(20.5/24)就是A區間,否則是B區間,这个簡單吧。
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;
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;
checkdt:=(present>(8.5/24))and(present<(20.5/24));
checkother:=(present>(20.5/24))and(present<(1));