使用COleDateTime类
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() ;
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
5) 把字符串转换为时间。
CString sDateTime;
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(sDateTime, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime sTime(nYear, nMonth, nDate, nHour, nMin, nSec);
要想知道更多的时间格式,参见MFC文档中的strftimehtml
使用COleDateTime类
1) 得到一年中的某一天。
COleDateTime datetime;
datetime = COleDateTime::GetCurrentTime();
int DayOfYear = datetime.GetDayOfYear();
2) 从文本串中读取时间。
COleDateTime datetime;
datetime.ParseDateTime("12:12:23 27 January 93");
3) 获取时间间隔。
//比方计算日期差
COleDateTime begin_date(1970, 1, 1, 0, 0, 0);
COleDateTime end_date(1990, 1, 1, 0, 0, 0);
COleDateTimeSpan timeSpan; //计算时间差
timeSpan = end_date - begin_date;
long expi_date = timeSpan.GetDays();sql
说明
■ CTime和COleDateTime具备几乎一样的功能。然而,COleDateTime容许用户得到一年中的某一天(建立Julian日期的一种好方法),以及分析一个时间文本串。
■ 与CTime相比, COleDateTime的优势在于它支持DWORD变量。COleDateTime使用的位数是双浮点的两倍,既然CTime只是简单地计算从1970年1月1日以后通过的秒数,因此到了2037年它将达到4294967295,从而不能再使用。相反,COleDateTime是一个
浮点数,它表示是从1900年12月30号以后的天数(小时是天的小数部分),几千年以内不会溢出。数据库
-------------------------------------------------------------------------------------------------------------------------------函数
CTime time = CTime::GetCurrentTime();spa int year = time.GetYear(); //返回年code int mouth = time.GetMouth(); //返回月份orm int date = time.GetDays(); // 返回日数htm int hour = time.GetHours(); // 返回小时数(-23至23之间) int sec = time.GetSeconds(); // 返回秒数(-59至59之间)事件 如下是转载的:CTime以及其它时间差 1、DateTimePicker控件 引用一个DateTimePicker控件,关联变量m_date为DateTimeCtrl类型,而后在响应事件中加入以下代码: CTime tm; 就能够获取了...可是不知为什么这个还和Vc的版本问题有关,开始时候我用的版本老是出错,或者srtDate为空,或者不是用户选择的日期,后来换了一个VC版本就能获得正确的数据了... 另外若是关联一个CTime类型的变量也能解决上面的问题,可是初始化的日期是1970-01-01而上面方法中初始化的日期是当前日期...因此根据用户须要的设定能够选择任何一种方法进行解决... 2、CTimeSpan获取时间差 要获取两个时间差,如两个CTime的时间差,可使用MFC中的CTimeSpan类。 CTime time1 = CTime::GetCurrentTime(); ...其它代码... CTime time2 = CTime::GetCurrentTime(); // 两个CTime相减获得CTimeSpan CTimeSpan timeSpan = time2 - time1; // 获得总的秒数 int nTSeconds = timeSpan.GetTotalSeconds();
注意GetTotalSeconds与GetSeconds的区别:GetTotalSeconds返回总的秒数,GetSeconds返回老是小于60,如:若是时间通过了100秒, GetTotalSeconds返回100,而GetSeconds返回40,由于有60秒转为一分钟了,同时使用GetMinutes会返回1,即1分40秒。 GetDays(); // 返回日数 GetHours(); // 返回小时数(-23至23之间) GetTotalHours(); // 返回总的小时数 GetMinutes(); // 返回分钟数(-59至59之间) GetTotalMinutes(); // 返回总的分钟数 GetSeconds(); // 返回秒数(-59至59之间) GetTotalSeconds(); // 返回总的秒数 3、CTime总结
1.初始化 m_begintime=CTime(2004,1,1,0,0,0,-1);//参数依次为year,month,day,hour,minite,second m_endtime =CTime::GetCurrentTime();//当前时间 2.日期比较 CTimeSpan span; span=time1-time2; 获得两时间的间隔. 能够取得span.GetHours().等 3.access数据库查询 使用DateDiff()函数,具体参照access帮助 CString timesql; timesql.Format(" Where DateDiff('d',%s,'%s')<=0","日期",m_begintime.Format("%Y-%m-%d")); 4读取日期字段(odbc) CDBVariant var; recset.GetFieldValue(i,var); s.Format("%d-%d-%d",(var.m_pdate)->year,(var.m_pdate)->month, (var.m_pdate)->day); 5.CTime转换为CString 例: m_begintime.Format("%Y-%m-%d");//2004-10-03 6.CString转换为CTime //s="2004-10-5" int first=s.Find('-'); int second=s.Find('-',first+1); int year=atoi(s.Left(4)); int month=atoi(s.Mid(first+1,second-first+1)); int day=atoi(s.Mid(second+1,s.GetLength()-second-1)); CTime temp(year,month,day,0,0,0); 7.判断CString是否表示的正确日期格式 //判断是否为2004-01-13 ch 可表明其余分隔符 bool IsDate(CString str,char ch) { if(str.IsEmpty()) return false; //日期分段 int first=str.Find(ch); int second=str.Find(ch,first+1); int year=atoi(str.Left(4)); int month=atoi(str.Mid(first+1,second-first+1)); int day=atoi(str.Mid(second+1,str.GetLength()-second-1)); //判断 if (year < 2000 || year >= 2010) { return false; } else if (month< 1 || month >12) { return false; } else if (day< 1 || day > 31) { return false; } else if (month == 4 || month == 6 || month == 9 || month == 11) { if(day > 30) { return false; } else { return true; } } else if (month == '2') { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { if (day>29) { return false; } else { return true; } } else if (day>28) { return false; } return true; } else { return true; } } 4、 在MFC中操做时间的类主要有两对:CTime和CTimeSpan与COleDateTime和COleDateTimeSpan,CTime和CTimeSpan主要封装了ANSI time_t和关于time_t的Run-Time库的主要函数,CTime里面使用的成员变量是time_t类型,该类型是个long型,因为long类型的缘由,因此该类只能处理4294967296秒约68年的数据,因此用CTime只能处理1970年到2038年的日期。 幸亏,MFC同时提供了COleDateTime和COleDateTimeSpan类,使用该两个类彻底能够代替CTime和CTimeSpan,COleDateTime和COleDateTimeSpan类所使用的成员变量是DATE类型,该类型是个double类型,并且使用的单位是日,因此能够处理从100年1月1日到9999年12月31日的日期时间,COleDateTime类的日期计算主要是操做公有成员变量COleDateTime::m_dt,该变量是DATE即double类型,该变量是为零时是1899年12月30日0时0分0秒,大于零时的日期比1899年12月30日0时0分0秒大,反之亦然,例如: COleDateTime t; t.m_dt=0; AfxMessageBox(t.Format("%Y-%m-%d %H:%M:%S")); 运行的结果是:1899-12-30 00:00:00 COleDateTime t; t.m_dt=39444.437731; AfxMessageBox(t.Format("%Y-%m-%d %H:%M:%S")); 运行的结果是:2007-10-28 10:30:20 反过来以能够获得变量的值,例如: COleDateTime t(2004,12,28,22,22,22); CString str; str.Format("%f",t.m_dt); AfxMessageBox(str); 运行的结果是:38349.932199 COleDateTimeSpan类是用于对COleDateTime类的两个时间的时间间隔的计算,COleDateTimeSpan类使用的成员变量COleDateTimeSpan::m_span是一个double类型是用于记录两个COleDateTime::m_dt的时间差,例如: COleDateTime t1(2006,1,1,0,0,0); COleDateTime t2(2007,1,1,0,0,0); COleDateTimeSpan ts=t2-t1; CString str; str.Format("%f",ts.m_span); AfxMessageBox(str); 运行的结果是:365.000000 反过来也能够获得日期 COleDateTime t1(2006,1,1,0,0,0); COleDateTimeSpan ts; ts.m_span=400.0; COleDateTime t2=t1+ts; AfxMessageBox(t2.Format("%c")); 运行的结果是:02/05/07 00:00:00 但是在使用COleDateTimeSpan类中如下的几个函数可要当心,这不知道是否是MFC的一个Bug, double GetTotalDays( ) const; double GetTotalHours( ) const; double GetTotalMinutes( ) const; double GetTotalSeconds( ) const; 几个函数的返回值都是double类型 可是,如double GetTotalSeconds( ) const;在MFC内部的原形是: _AFXDISP_INLINE double COleDateTimeSpan::GetTotalSeconds() const { ASSERT(GetStatus() == valid); long lReturns = (long)(m_span * 24 * 60 * 60 + AFX_OLE_DATETIME_HALFSECOND); return lReturns; } 看到没有,它返回的实际是个long类型,并非一个double类型,因此在使用这几个函数的时候计算两个时间的间隔不要太大,特别是GetTotalSeconds( )函数,如计算两个时间的间隔大于68年时就会溢出,因此我建议直接读取COleDateTimeSpan::m_span变量的值,这是一个单位为日的时间间隔,例如: COleDateTime t1(2000,1,1,0,0,0); COleDateTime t2(2070,1,1,0,0,0); COleDateTimeSpan ts=t2-t1; CString str; str.Format("%f",ts.GetTotalSeconds()); AfxMessageBox(str); 运行的结果是:-2085892096.000000 这个结果明显是一个溢出,若是使用 str.Format("%f",ts.m_span*86400); 则会获得2209075200.000000。 因此灵活使用COleDateTime类的m_dt变量和COleDateTimeSpan类的m_span变量操做会获得意想不到的收获
|