时间类CTime类和CTimeSpan类
记得添加头文件“#include "atltime.h"”
CTime类的对象表示的时间是基于格林威治标准时间(GMT)的。
CTimeSpan类的对象表示的是时间间隔。
常用构造函数如下,最后一个参数可以默认-1。
CTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1 );
比如:CTime mytime=CTime(2019,3,14,12,35,45)//2019年3月14日12点35分45秒
获取系统时间:CTime curtime = CTime::GetCurrentTime();
获取年月日时分秒以及星期的方法:curtime是时间对象
int week = curtime.GetDayOfWeek();
int nYear = curtime.GetYear(); // 获取当前年份
int nMonth = curtime.GetMonth(); // 获取当前月份
int nDay = curtime.GetDay(); // 获取当前日期
int nHour = curtime.GetHour(); // 获取当前小时时间
int nMin = curtime.GetMinute(); // 获取当前分钟时间
int nSec = curtime.GetSecond(); // 获取当前秒时间
将时间结构转变为字符串结构可以直接用函数也可以用格式输出
CString Cmytime =curtime.Format("%Y年%m月%d日%H时%M分%S秒")
Cmytime.Format(_T("%d年%d月%d日%d时%d分%d秒,星期%d"), nYear, nMonth, nDay, nHour, nMin, nSec,week);
可以使用加减等运算符重载
CTime operator ±(CTimeSpan timeSpan) const;//Ctime类对象加上CTimeSpan类对象,返回一个Ctime类对象
CTime& operator ±=(CTimeSpan span);//为一个Ctime类对象加上一段时间
CTimeSpan operator -(CTime time) const;//将该CTime对象和另一个CTime对象相减,返回一个CTimeSpan对象。
以及判断相等与比较
CTimeSpan类的主要成员函数
CTimeSpan( LONG lDays, int nHours, int nMins, int nSecs );
GetSeconds():获得当前分钟包含的秒数,取值范围为-59到59。
GetTotalSeconds():获得CTimeSpan类对象中包含的完整的秒数。
CString Format(LPCTSTR pszFormat) const;
CTime firtime = CTime::GetCurrentTime();
CTimeSpan myCTimeSpan = CTimeSpan(0, 0, 1, 3);
CTime curtime = firtime + myCTimeSpan;
CString Cmytime = curtime.Format("%Y年%m月%d日%H时%M分%S秒");
MessageBox(Cmytime);