【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒)
CString str;
//获取系统时间
CTime tm;
tm=CTime::GetCurrentTime();//获取系统日期
str=tm.Format(“现在时间是%Y年%m月%d日 %X”);
MessageBox(str,NULL,MB_OK);
a,从CTimet中提取年月日时分秒
CTime t = CTime::GetCurrentTime();
int d=t.GetDay(); //获得几号
int y=t.GetYear(); //获取年份
int m=t.GetMonth(); //获取当前月份
int h=t.GetHour(); //获取当前为几时
int mm=t.GetMinute(); //获取分钟
int s=t.GetSecond(); //获取秒
int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六
b,计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:
CTime t1( 1, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔
int iDay=span.GetDays(); //获取这段时间间隔共有多少天
int iHour=span.GetTotalHours(); //获取总共有多少小时
int iMin=span.GetTotalMinutes();//获取总共有多少分钟
int iSec=span.GetTotalSeconds();//获取总共有多少秒
c,获得当前日期和时间,并可以转化为CString
CTime tm=CTime::GetCurrentTime();
CString str=tm.Format(“%Y-%m-%d”);//显示年月日
2.使用GetLocalTime:Windows API 函数,获取当地的当前系统日期和时间 (精确到毫秒)
此函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边
typedef struct _SYSTEMTIME
{
WORD wYear;//年
WORD wMonth;//月
WORD wDayOfWeek;//星期:0为星期日,1为星期一,2为星期二……
WORD wDay;//日
WORD wHour;//时
WORD wMinute;//分
WORD wSecond;//秒
WORD wMilliseconds;//毫秒
}SYSTEMTIME,*PSYSTEMTIME;
例:
SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format(“%4d-%2d-%2d”,st.wYear,st.wMonth,st.wDay);
strTime.Format(“%2d:%2d:%2d”,st.wHour,st.wMinute,st.wSecond) ;
AfxMessageBox(strDate);
AfxMessageBox(strTime);
3.使用GetTickCount:从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。(精确到毫秒)
//获取程序运行时间
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
Sleep(500);
long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)
str.Format(“time:%dms”,t2-t1);//前后之差即 程序运行时间
AfxMessageBox(str);
//获取系统运行时间
long t=GetTickCount();
CString str,str1;
str1.Format(“系统已运行 %d时”,t/3600);
str=str1;
t%=3600;
str1.Format(“%d分”,t/60);
str+=str1;
t%=60;
str1.Format(“%d秒”,t/1);
str+=str1;
AfxMessageBox(str);
4.使用time_t time( time_t * timer ) : 仅使用C标准库(精确到秒)
得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数
计算时间差:double difftime( time_t timer1, time_t timer0)
struct tm *localtime(const time_t *timer); 取得当地时间,localtime获取的结果由结构tm返回
返回的字符串可以依下列的格式而定:
%a 星期几的缩写。Eg:Tue
%A 星期几的全名。 Eg: Tuesday
%b 月份名称的缩写。
%B 月份名称的全名。
%c 本地端日期时间较佳表示字符串。
%d 用数字表示本月的第几天 (范围为 00 至 31)。日期
%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。
%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。
%j 以数字表示当年度的第几天 (范围为 001 至 366)。
%m 月份的数字 (范围由 1 至 12)。
%M 分钟。
%p 以 ”AM” 或 ”PM” 表示本地端时间。
%S 秒数。
%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。
%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。
%w 用数字表示本周的第几天 ( 0 为周日)。
%x 不含时间的日期表示法。
%X 不含日期的时间表示法。 Eg: 15:26:30
%y 二位数字表示年份 (范围由 00 至 99)。
%Y 完整的年份数字表示,即四位数。 Eg:2008
%Z(%z) 时区或名称缩写。Eg:中国标准时间
%% % 字符。
含:年,月,日,周几,小时,分,秒,毫秒。[喝小酒的网摘]http://blog.const.net.cn/a/16370.htm
【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法的更多相关文章
- VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是 <sys/time.h> . 函数说明 int gettimeofday ...
- VC++编程中获取系统时间
<span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...
- vc 获取当前时间 (zhuan)
vc 获取当前时间(2010-02-10 11:34:32) http://wenku.baidu.com/view/6ade96d049649b6648d7475e.html 1.使用CTime类 ...
- vc 获取当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime:: GetCurrentTime_r(); str=tm.Format("现在时间是% ...
- 用PHP获取系统时间时,时间比当前时间少8个小时
自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...
- C/C++获取系统时间
C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...
- c++ 怎样获取系统时间
c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...
- Android获取系统时间的多种方法
Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...
随机推荐
- 【vue】@click绑定的函数,如何同时传入事件对象和自定义参数
知识很久不用的话,果然是容易忘的... 记记笔记,希望能加深点印象吧. [仅仅传入事件对象] html: <div id="app"> <button @clic ...
- Call JMS Web Service
The content type application/json of the response message does not match the content type of the bin ...
- MSMQ介绍
最近的项目中用到了MSMQ,简单的使用到了它,现总结下.有些是网上的资料有些是自己的笔记. MSMQ理解 Message Queue(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式 ...
- P4779 【模板】单源最短路径(标准版)题解
原题链接 https://www.luogu.org/problemnew/show/P4779 若还未食用弱化版的同学请先做这个qwq https://www.luogu.org/problemne ...
- codeforces#1150D. Three Religions(dp+序列自动机)
题目链接: https://codeforces.com/contest/1150/problem/D 题意: 给出长度为$n$的字符串,和$q$次询问 每次询问是,给$x$宗教增加一个字符$key$ ...
- 刘汝佳dicnic模板
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #in ...
- 状压dp之不相连块
传送门 一块田里草地格子不能相邻,问有几种方案. 预处理不相邻块 #include<iostream> #include<cstdio> #include<algorit ...
- asp.net webAPI
Get: 1.Get参数传递的本质是url字符串拼接:2.url字符串长度受限制:3.Get参数传递在Http请求头部传递,而不支持Request-Body传递:4.Get类型的方法支持参数为基本类型 ...
- ORA-12899 导入失败
主要是目标数据库的字符集与导入文件的字符集不符 SQL>SHUTDOWN IMMEDIATE SQL>STARTUP MOUNT SQL>ALTER SYSTEM ENABLE RE ...
- 查一张表占多少空间Bytes
SELECT SUM(BYTES)/1024/1024||'MB' 占用空间 FROM dba_segments WHERE segment_name = '表名' AND owner = '用户名' ...