判断一个时间点是否在一个精确到年月日时分秒的时间段里,该怎么解决
判断一个时间点是否在一个精确到年月日时分秒的时间段里
各位:
我现在做的一个查询,需要客户选择起始的时间(年月日时分秒),来对数据库中的记录进行查询。
系统(不是我写的)本来已经实现了起始时间(年月日)的查询,我现在要做的就是扩充一下,(可以肯定数据库中有到时分秒的记录)窗口上原本放了两个dateTimePicker控件,分别取名为:sDateFr和sDateTo。
相关代码为:
sDateFr = CompressDate(YearOf(DateTimeFr->DateTime) % 100,MonthOf(DateTimeFr->DateTime),DayOf(DateTimeFr->DateTime));
sDateTo = CompressDate(YearOf(DateTimeTo->DateTime) % 100,MonthOf(DateTimeTo->DateTime),DayOf(DateTimeTo->DateTime));
其中CompressDate函数是这样定义的:
int __fastcall TEcrLogForm::CompressDate(BYTE pYear,BYTE pMonth,BYTE pDay)
{
return ((int)INT2BCD(pYear)<<16)+((int)INT2BCD(pMonth)<<8)+INT2BCD(pDay);
}
INT2BCD是自己定义的函数,功能顾名思义。
把这两个起止时间组装完了之后
BYTE sYear,sMonth,sDay,sHour,sMinutes,sSecond; (BYTE是自己定义的类型)
sYear = FlowBuff.CDC_regis.FYear;
sMonth = FlowBuff.CDC_regis.FMonth;
sDay = FlowBuff.CDC_regis.FDay;
sHour = FlowBuff.CDC_regis.FHour;
sMinutes = FlowBuff.CDC_regis.FMinute;
sSecond = FlowBuff.CDC_regis.FSecond;
sDateLog = (((int)FlowBuff.CDC_regis.FYear)<<16) + ((int)FlowBuff.CDC_regis.FMonth<<8)+FlowBuff.CDC_regis.FDay;
if (sDateLog>=sDateFr && sDateLog<=sDateTo)
sActLog = true;
就可以开始查询了。
我现在的理解是:精确到时分秒应该是可以查到的,我又放了两个DateTimePicker控件,用来选择起止时间,用HourOf()、MinuteOf()、SecondOf()进行处理,问题是:我该怎么组装它们?该怎么跟已有的时间(年月日)联系起来?查询开始前的if语句怎么判断?
------解决方案--------------------
数据库里面的时间是什么格式的?是年月日时分秒一个字段?如果是的话,你可以将两个控件的时间合起来再进行比较。年月日+时分秒
TDateTime TempTime;
TempTime.FormatString("%d-%d-%d %d:%d:%d",iYear,iMonth,iDay,iHour,iMinite,iSecond)
这样转换成一个完整的时间。
------解决方案--------------------
各位:
我现在做的一个查询,需要客户选择起始的时间(年月日时分秒),来对数据库中的记录进行查询。
系统(不是我写的)本来已经实现了起始时间(年月日)的查询,我现在要做的就是扩充一下,(可以肯定数据库中有到时分秒的记录)窗口上原本放了两个dateTimePicker控件,分别取名为:sDateFr和sDateTo。
相关代码为:
sDateFr = CompressDate(YearOf(DateTimeFr->DateTime) % 100,MonthOf(DateTimeFr->DateTime),DayOf(DateTimeFr->DateTime));
sDateTo = CompressDate(YearOf(DateTimeTo->DateTime) % 100,MonthOf(DateTimeTo->DateTime),DayOf(DateTimeTo->DateTime));
其中CompressDate函数是这样定义的:
int __fastcall TEcrLogForm::CompressDate(BYTE pYear,BYTE pMonth,BYTE pDay)
{
return ((int)INT2BCD(pYear)<<16)+((int)INT2BCD(pMonth)<<8)+INT2BCD(pDay);
}
INT2BCD是自己定义的函数,功能顾名思义。
把这两个起止时间组装完了之后
BYTE sYear,sMonth,sDay,sHour,sMinutes,sSecond; (BYTE是自己定义的类型)
sYear = FlowBuff.CDC_regis.FYear;
sMonth = FlowBuff.CDC_regis.FMonth;
sDay = FlowBuff.CDC_regis.FDay;
sHour = FlowBuff.CDC_regis.FHour;
sMinutes = FlowBuff.CDC_regis.FMinute;
sSecond = FlowBuff.CDC_regis.FSecond;
sDateLog = (((int)FlowBuff.CDC_regis.FYear)<<16) + ((int)FlowBuff.CDC_regis.FMonth<<8)+FlowBuff.CDC_regis.FDay;
if (sDateLog>=sDateFr && sDateLog<=sDateTo)
sActLog = true;
就可以开始查询了。
我现在的理解是:精确到时分秒应该是可以查到的,我又放了两个DateTimePicker控件,用来选择起止时间,用HourOf()、MinuteOf()、SecondOf()进行处理,问题是:我该怎么组装它们?该怎么跟已有的时间(年月日)联系起来?查询开始前的if语句怎么判断?
------解决方案--------------------
数据库里面的时间是什么格式的?是年月日时分秒一个字段?如果是的话,你可以将两个控件的时间合起来再进行比较。年月日+时分秒
TDateTime TempTime;
TempTime.FormatString("%d-%d-%d %d:%d:%d",iYear,iMonth,iDay,iHour,iMinite,iSecond)
这样转换成一个完整的时间。
------解决方案--------------------
- C/C++ code
TDateTime time; AnsiString s1,s2,s3; s1=DateTimePicker2->Date.FormatString("yyyy-MM-dd"); s2=DateTimePicker1->Time.FormatString("hh:mm:ss"); s3=s1+" "+s2; time=StrToDateTime(s3); Button1->Caption=time.FormatString("yyyy-MM-dd hh:mm:ss");