ctimespan(如何用CTime进行时间加减法)

本文目录
如何用CTime进行时间加减法
昨天:
CTime m_Date = CTime::GetCurrentTime() - CTimeSpan( 1, 0, 0, 0 );
明天
CTime m_Date = CTime::GetCurrentTime() + CTimeSpan( 1, 0, 0, 0 );
CTimeSpan的第一个参数换成其他数值就可以计算若干天之前或之后的日期了。
c语言中时间处理
1. ctime函数
函数: ctime
功 能: 把日期和时间转换为字符串
用 法: char *ctime(const time_t *time);
程序例:
#include《cstdio》
#include《ctime》
intmain(void)
{
time_tt;
t=time(&t);
printf("Today’sdateandtime:%s\n",ctime(&t));
return0;
}
注:若在linux下使用本函数,需要include 《time.h》头文件
2.CTime类
CTime类的对象表示的时间是基于格林威治标准时间(GMT)的。CTimeSpan类的对象表示的是时间间隔。
CTime类一般不会被继承使用。其对象的大小是8个字节。
CTime表示的日期上限是2038年1月18日,下限是1970年1月1日 12:00:00 AM GMT。
CTime类的成员函数
CTime();
构造一个未经初始化的CTime对象。此构造函数使我们可以定义一个CTime对象的数组,在使用数组前需要以有效的时间值为其初始化。
CTime(__time64_t time);
以一个__time64_t(注意:最前面的下划线有两条)类型的数据来构造一个CTime对象。参数time是一个__time64_t类型的值,表示自GMT时间1970年1月1日零点以来的秒数,这里要注意的是,参数time代表的时间会转换为本地时间保存到构造的CTime对象中。例如,我们传递参数0构造一个CTime对象,然后调用CTime对象的GetHour成员函数将返回8,因为参数0代表的GMT时间转换为北京时间后为1970年1月1日 8:00:00。
CTime(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
int nDST = -1
);
以本地时间的年、月、日、小时、分钟、秒等几个时间分量构造CTime对象。参数nYear、nMonth、nDay、nHour、nMin、nSec分别表示年、月、日、小时、分钟、秒,取值范围如下:
时间分量 取值范围
nYear 1970-3000
nMonth 1-12
nDay 1-31
nHour 0-23
nMin 0-59
nSec 0-59
参数nDST指定是否实行夏令时,为0时表示实行标准时间,为正数时表示实行夏令时,为负数时由系统自动计算实行的是标准时间还是夏令时。
CTime(const SYSTEMTIME& st,int nDST = - 1) ;
以一个SYSTEMTIME结构体变量来构造CTime对象。SYSTEMTIME结构体也是我们对日期时间的常用表示方式。参数st为以本地时间表示的SYSTEMTIME对象,参数nDST同上。
static CTime WINAPI GetCurrentTime( );
获取系统当前日期和时间。返回表示当前日期和时间的CTime对象。
int GetYear( ) const;
获取CTime对象表示时间的年份。范围从1970年1月1日到2038年(包括2038年)1月18日。
int GetMonth( ) const;
获取CTime对象表示时间的月份。范围为1到12。
int GetDay( ) const;
获取CTime对象表示时间的日期。范围为1到31。
int GetHour( ) const;
获取CTime对象表示时间的小时。范围为0到23。
int GetMinute( ) const;
获取CTime对象表示时间的分钟。范围为0到59。
int GetSecond( ) const;
获取CTime对象表示时间的秒。范围为0到59。
int GetDayOfWeek( ) const;
此函数的返回值表示CTime对象代表的是星期几,1表示是周日,2表示是周一,以此类推。
CString Format(LPCTSTR pszFormat) const;
将CTime对象中的时间信息格式化为字符串。参数pszFormat是格式化字符串,与printf中的格式化字符串类似,格式化字符串中带有%前缀的格式码将会被相应的CTime时间分量代替,而其他字符会原封不动的拷贝到返回字符串中。格式码及含义如下:
%a:周的英文缩写形式。
%A:周的英文全名形式。
%b: 月的英文缩写形式。
%B:月的英文全名形式。
%c: 完整的日期和时间。
%d:十进制形式的日期(01-31)。
%H:24小时制的小时(00-23)。
%I: 12小时制的小时(00-11)。
%j: 十进制表示的一年中的第几天(001-366)。
%m: 月的十进制表示(01-12)。
%M:十进制表示的分钟(00-59)。
%p: 12小时制的上下午标示(AM/PM)。
%S: 十进制表示的秒(00-59)。
%U: 一年中的第几个星期(00-51),星期日是一周的第一天。
%W: 一年中的第几个星期(00-51),星期一是一周的第一天。
%w: 十进制表示的星期几(0-6)。
%Y: 十进制表示的年。
CTime operator +(CTimeSpan timeSpan) const;
将CTime对象和CTimeSpan对象相加,返回一个CTime对象。实际意义就是在一个时间的基础上推后一个时间间隔,得到一个新的时间。
CTime operator -(CTimeSpan timeSpan) const;
将CTime对象和一个CTimeSpan相减,返回一个CTime对象。实际意义就是在一个时间的基础上提前一个时间间隔,得到一个新的时间。
CTimeSpan operator -(CTime time) const;
将该CTime对象和另一个CTime对象相减,返回一个CTimeSpan对象。实际意义就是计算两个时间点的间隔,得到一个CTimeSpan对象。
CTime& operator +=(CTimeSpan span);
为该CTime对象增加一个span表示的时间间隔。
CTime& operator -=(CTimeSpan span);
为该CTime对象减去一个span表示的时间间隔。
CTime& operator =(__time64_t time);
为该CTime对象赋予一个新的时间值。
简单说下剩下的几个重载i运算符:
operator == : 比较两个绝对时间是否相等。
operator != : 比较两个绝对时间是否不相等。
operator 》 : 比较两个绝对时间,是否前一个大于后一个。
operator 《 : 比较两个绝对时间,是否前一个小于后一个。
operator 》= : 比较两个绝对时间,是否前一个大于等于后一个。
operator 《= : 比较两个绝对时间,是否前一个小于等于后一个。
=====================================================================
C++中,CTime 与 CString转换
CTime m_StartTime1 = CTime::GetCurrentTime();
CString csStartTime = m_StartTime1.Format( "%Y%m%d%H%M%S" );
一.将CString转为CTime的几种方法
CString timestr = "2000年04月05日";
int a,b,c ;
sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);
CTime time(a,b,c,0,0,0);
--------or - ---------------------
CString s("2001-8-29 19:06:23");
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
---- or ------------------------
CString timestr = "2000年04月05日";
int year,month,day;
BYTE tt;
//get year
memset(tt, 0, sizeof(tt));
tt;
tt;
tt;
tt;
year= atoi((char *)tt);
//get month
memset(tt, 0, sizeof(tt));
tt;
tt;
month = atoi((char *)tt);
//get day
memset(tt, 0, sizeof(tt));
tt;
tt;
day = atoi((char *)tt);
CTime time(year,month,day,0,0,0);
从上面来看,很明显使用sscanf()函数的优势.
二.将CTIme转换为CString的方法:
CTime tmSCan = CTime::GetCurrentTime();
CString szTime = tmScan.Format("’%Y-%m-%d %H:%M:%S’");
这样得到的日期时间字符串就是以"2006-11-27 23:30:59"的格式.这是不是很方便呢?
//取得CTime中的日期
CString cstrDate = tmScan.Format("%Y-%m-%d");
//取得CTime中的时间
CString cstrTime = tmScan.Format("%H:%M-%S");
sprintf还有个不错的表妹:strftime,专门用于格式化时间字符串的,用法跟她表哥很像,也是一大堆格式控制符,只是毕竟小姑娘家心细,她还要调用者指定缓冲区的最大长度,可能是为了在出现问题时可以推卸责任吧。这里举个例子:
更多更好的sprintf()函数说明参考:《spirntf,你知道多少?》
time_t t = time(0);
//产生"YYYY-MM-DD hh:mm:ss"格式的字符串。
char s;
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t));
sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,这一对由于从面向对象哪里得到了赞助,用以写出的代码更觉优雅
C++中用time_t保存在文件中的时间与现在的系统时间时间差计算
ctime已经重载过“-”了,减完了之后用CTimeSpan 的GetTotalSeconds( )GetTotalMinutes( )等 得到具体时间
下面是MSDN:
CTime::operator +, -
CTime operator +( CTimeSpan timeSpan ) const;
CTime operator -( CTimeSpan timeSpan ) const;
CTimeSpan operator -( CTime time ) const;
Remarks
CTime objects represent absolute time. CTimeSpan objects represent relative time. The first two operators allow you to add and subtract CTimeSpan objects to and from CTime objects. The third allows you to subtract one CTime object from another to yield a CTimeSpan object.
Example
// example for CTime::operator +, -
CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999
CTimeSpan ts = t2 - t1; // Subtract 2 CTimes
ASSERT( ts.GetTotalSeconds() == 86400L );
ASSERT( ( t1 + ts ) == t2 ); // Add a CTimeSpan to a CTime.
ASSERT( ( t2 - ts ) == t1 ); // Subtract a CTimeSpan from a Ctime.
CTime Overview | Class Members | Hierarchy Chart

更多文章:
excel+条件格式设置好第一行批量(excel条件格式批量)
2026年8月31日 07:00
国内外贸电商有没有什么好系统可以建站的?做电商软件的企业有哪些
2026年7月3日 07:30
获取request对象(java怎么获取request对象)
2025年5月31日 06:15
工作流技术有哪些(翼发云OA办公系统用的什么技术来实现可视化工作流的)
2025年8月2日 22:15
index第二个参数的含义(如何使用index和match函数:)
2025年12月18日 13:00
alter table rename to是什么命令(如何给列重命名 SQL)
2026年8月28日 21:30
oracle19c32位客户端下载(oracle32位客户端以64位运行怎么解决)
2026年5月16日 14:00
get the actors positioned(高中英语作文:落下的书本 The Left Books)
2026年7月23日 16:15














