请教一个日期时间区间判断的问题。着急在线等答复!

yeweimian1 2006-03-17 12:12:16
从系统获取日期时间,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区间。

这个算法如何写比较简洁呢?请教各位。谢谢!
...全文
250 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
leeky 2006-03-19
  • 打赏
  • 举报
回复
呵呵,可能代碼風格不好,我沒仔細看,但覺得不必那麽长的代碼。
cuteant 2006-03-17
  • 打赏
  • 举报
回复
给你一个思路吧,将Now减去8:30,将时间部分和0.5比较,大于就是B,小于就是A

然后将日期部分的月和日取出来,三个组合。。。

应该没有难点,还有疑问请提出。
yeweimian1 2006-03-17
  • 打赏
  • 举报
回复
其实有用的就是下面几句:

present:=now()-date();
checkdt:=(present>(8.5/24))and(present<(20.5/24));
checkother:=(present>(20.5/24))and(present<(1));
yeweimian1 2006-03-17
  • 打赏
  • 举报
回复
楼上的不对。

2006.3.15 23:20:22应该是0315B吧?你的还是0315A。

2006.3.16 05:00:00应该是0315B吧?你的还是0315A。
cuteant 2006-03-17
  • 打赏
  • 举报
回复
搞这么复杂的程序做什么呢?

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)));

这样就可以了
cuteant 2006-03-17
  • 打赏
  • 举报
回复
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)));
yeweimian1 2006-03-17
  • 打赏
  • 举报
回复
感谢网友: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;
ruthfox 2006-03-17
  • 打赏
  • 举报
回复
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;
leeky 2006-03-17
  • 打赏
  • 举报
回复
哈哈,表達式歧義了,時間日期,就不要用字符串嘛,直接TDateTime,浮点数操作。
當前時間Now()減去自己的整數部分(会取整吧),結果如果>=(8.5/24)而且<=(20.5/24)就是A區間,否則是B區間,这个簡單吧。
chenylin 2006-03-17
  • 打赏
  • 举报
回复
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;
postren 2006-03-17
  • 打赏
  • 举报
回复
那‘121B’
是1月21日呢,还是12月1日

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧