时间类型变量的处理总结

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

日期时间类变量的处理

C++编程时对日期时间类变量的处理一般采用CTime与COleDateTime CTime类

1) 获取当前时间。

CTime time;

time = CTime::GetCurrentTime();

2) 获取时间元素。

int year = time.GetYear() ;

int month = time.GetMonth();

int day = time.GetDay();

int hour = time.GetHour();

int minute = time.GetMinute();

int second = time.GetSecond();

int DayOfWeek = time.GetDayOfWeek() ;

Returns the day of the week based on local time; 1 = Sunday, 2 = Monday, to 7 = Saturday 3) 获取时间间隔。

CTimeSpan timespan(0,0,1,0); // days,hours,minutes,seconds

timespan = CTime::GetCurrentTime() - time;

4) 把时间转换为字符串。

CString sDate,sTime,sElapsed Time ;

sDate = time.Format("%m/%d/%y"); //ex: 12/10/98

sTime = time.Format("%H:%M:%S"); //ex: 9:12:02

sElapsed Time = timespan.Format("%D:%H:%M:%S");

// %D is total elapsed days

要想知道更多的时间格式,参见M F C文档中的strftime。

COleDateTime类

1) 获得一年中的某一天。

COleDate Time datetime;

datetime = COleDateTime::GetCurrentTime();

int DayOfYear = datetime.GetDayOfYear();

2) 从文本串中读取时间。

COleDate Time datetime;

datetime.ParseDateTime("12:12:23 27 January 93");

说明:CTime和COleDateTime具有几乎同样的功能。然而COleDateTime允许用户获得一年中的某一天(创建Julian日期的一种好方法),以及分析一个时间文本串。与CTime相比,COleDateTime的优点在于它支持DWORD变量。COleDateTime使用的位数是双浮点的两倍,既然CTime只是简单地计算从1970年1月1日之后经过的秒数,

CTime是无符号long类型,它的范围是0-4 2 9 4 9 6 7 2 9 5;

COleDateTime是double类型,它占64位。

将界面录入的时间存入数据库的时间日期字段:

即如何把COleDateTime 型转化成为 _variant_t型

COleVaraint var=COleDateTime(2001,11,25,0,12,34,56);

_variant_t var1=var;

来源:电脑网络爱好者:/viewnews-24290.html

ADO数据库编程的例子

_variant_t var,varD;

double time;

COleDateTime timeD,timeD1;

COleDateTimeSpan timespan2(0,0,90,0);

CString strSQL,strTem,strTem1;

varD=TAR.m_pRecordset->GetCollect("DATATIME");

time=varD.date;

timeD=time;

//strTime=timeD.Format("%y年%m月%d日%H:%M:%S");

strTem.Format("%d-%d-%d %d:%d:%d",timeD.GetYear(),timeD.Get Month(),timeD.GetDay(),timeD.GetHour(),timeD.GetMinute(),timeD.GetS econd());

timeD1=timeD-timespan2;

strTem1.Format("%d-%d-%d %d:%d:%d",timeD1.GetYear(),timeD1.Get Month(),timeD1.GetDay(),timeD1.GetHour(),timeD1.GetMinute(),timeD1.Get Second());

strSQL.Format("select * from BOC where DATATIME between to_date('%s','yyyy-mm-dd hh24:mi:ss') and to_date('%s','yyyy-mm-dd hh24:mi:ss') order by DATATIME DESC",strTem1,strTem);

说明:首先用m_pRecordset->GetCollect()获取时间值,返回类型是_variant_t,_variant_t则有date的成员变量,但是是double类型的,这个double类型的数据可以直接赋值给COleDateTime类型,最后完成了从数据库时间类型到VC++得时间类型的转化,COleDateTimeSpan则可以对时间的间隔进行各种操作,但获取总的秒数间隔是最准确,最有用的。

在用SQL语言进行查询时,to_date函数可以把字符型的时间转化为oracle数据库的日期型。例如:select * from CDUMP where DAT_END between to_date('2009-12-11 13:11:43','yyyy-mm-dd hh24:mi:ss')-1/24and to_date('2009-12-11 13:11:43','yyyy-mm-dd hh24:mi:ss')。其中的1/24表示一个小时,查询的是2009-12-11 13:11:43 1小时前的数据,需要注意的是前一个时间一定要比后一个早。'2009-12-11 13:11:43'是字符型的。

例子:

select DAT_END from CDUMP where (extract(year from DAT_END) between 2009 and 2009)

and (extract(month from DAT_END) between 12 and 12)

and (extract(day from DAT_END) between 11 and 11) and (to_char(DAT_END,'HH24') between 12 and 13)

select DAT_END from CDUMP where (extract(year from DAT_END) between %d and %d)

相关文档
最新文档