函数: ctime 功 能: 把日期和时间转换为字符串 用 法: char *ctime(const time_t *time); 程序例: #include <cstdio> #include <ctime> int main(void) { time_t t; t=time(&t); printf("Today's date and time: %s\n", ctime(&t)); return 0; } 注:若在linux下使用本函数,需要include <time.h>头文件 -------------------------------------------------------------------------------------------------------------------- 类: CTime() 创建CTime对象, 使他的时间为当前时间。 类函数: GetMinute() 得到分钟. GetSecond() 得到秒; GetHour() 得到小时; GetDay() 得到 CTime持有的"天" ; GetMonth() 得到月; GetDayOfWeek() 得到 CTime持有的"天"是一星期中的那一天 ; GetYear() 得到年; GetTime() 返回用 __time32_t 表示的时间; 在VC6下只支持到2039年,也就是2的32次方秒 例: CTime T; int year; T=CTime(2008,8,8,8,8,8); //设置时间为2008年8月8号8时8分8秒 //或者 CTime T =CTime::GetCurrentTime(); //设置为当前时间 year=t.GetYear(); //获取年份 ===================================================================== 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[5]; //get year memset(tt, 0, sizeof(tt)); tt[0] = timestr[0]; tt[1] = timestr[1]; tt[2] = timestr[2]; tt[3] = timestr[3]; year= atoi((char *)tt); //get month memset(tt, 0, sizeof(tt)); tt[0] = timestr[6]; tt[1] = timestr[7]; month = atoi((char *)tt); //get day memset(tt, 0, sizeof(tt)); tt[0] = timestr[10]; tt[1] = timestr[11]; 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[32]; strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t)); sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,这一对由于从面向对象哪里得到了赞助,用以写出的代码更觉优雅
转载于:https://www.cnblogs.com/owenyang/archive/2012/08/28/3579114.html
C++CTime使用方法相关推荐
- MFC FILETIME和CTime相互转换的处理
FILETIME和CTime相互转换的处理方法: FILETIME转CTime的方法: 1.CTime对象在初始化时可以传递FILETIME结构 FILETIME ft: CTime time(ft) ...
- Python 多进程 multiprocessing 使用示例
参考:http://blog.csdn.net/qdx411324962/article/details/46810421 参考:http://www.lxway.com/4488626156.htm ...
- CFileFind类简介
CFileFind类 MFC类CFileFind执行本地文件查找,是CGopherFileFind和CFtpFileFind的基类:后两类用于Internet文件查找.CFileFind包括的成员函数 ...
- Python-内置函数
1.abs(x):返回数字的绝对值,x可以是整数.浮点数.复数: 注:若x是复数,则返回其大小 import math a = -1 b = -1.3232 c = b d = 1+1.0j e = ...
- C++11 中chrono库 实现高精度定时
一种"传统"ctime计时方法: #include <ctime> using namespace std;clock_t start = clock(); // do ...
- MFC或C/C++中如何判断目录存在,文件/文件夹存在,亦或是文件夹存在,文件存在
判断的方式太多太多,这里暂时列举5中方式. 在文章开始之前,由于需要用到CString转char功能,所以先介绍一个CString转char的方法: 想知道更多参见CString与char *互转总结 ...
- C++精华基础贴[水木清华]
导读: 发信人: CDRAGON (CDRAGON), 信区: CPlusPlus 标 题: 基础知识 发信站: BBS 水木清华站 (Mon Nov 20 13:03:14 2006), 站内 八进 ...
- ]C++精华基础贴[水木清华]
发信人: CDRAGON (CDRAGON), 信区: CPlusPlus 标 题: 基础知识 发信站: BBS 水木清华站 (Mon Nov 20 13:03:14 2006), 站内 八进制:以 ...
- Python之日期与时间处理模块(date和datetime)
本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常需要用到日期与时间,如: 作为日志信息的内容输出 计算某个功能的执行时 ...
最新文章
- 创业互联网公司如何搭建自己的技术架构
- 基于梯度下降法的——线性回归拟合
- 介绍一个免费的采用人工智能放大老照片的在线网站
- Linux rm 删除指定文件外的其他文件 方法汇总
- windows server 2008 + IIS 7.5实现多用户FTP(多账号对应不同目录)
- 关于cell中添加子视图 复用重叠问题的解决方法
- Springboot导出excel工具类
- 【blender】基本操作总结-最常用的快捷键
- UG工程图模板制作方法,超级实用
- springboot优缺点
- NAS存储忘记密码如何解决?
- 计算机汉字录入试题,上机试题:汉字录入题.doc.doc
- 蓝桥杯0027 通信密码
- BCIduino社区|HY-BCI Pro多通道科研级脑电放大器接收lsl脑电数据并进行显示
- 最优化学习 拟牛顿法(Quasi-Newton Method)
- 静态数组,全局数组,局部数组的初始化区别
- 哈罗要在网约车市场取得突破,离开补贴行不通
- python之signal操作
- Linux df命令怎么使用
- W32TM注册time.windows.com作为权威时间同步源
热门文章
- 玩cf出现outofmemory_《穿越火线》的肖枫原型是CF选手白鲨吗?
- SPringMVC使用总结
- 华为交换机同步linux时间服务器,华为s5720s系列交换机同步时间
- IDEA创建maven JavaWeb工程
- 网络适配器无法建立连接_「图」KB4515384更新令人抓狂:网络适配器又无法启用...
- pcss评分_GTA5画质设置 N卡画质选项设置指南
- 装了mysql但是服务里没有反应_Mysql安装后在服务里找不到和服务启动不起来的解决方法...
- linux链接curl库,Linux利用curl库快速开发http应用
- spring boot 拦截器获取controller返回的数据_高级码农Spring Boot实战与进阶之过滤器和拦截器的使用及其区别...
- python里help和dir的区别_Python中dir()与help()的使用