1. Windows下CC获取当前系统时间
  2. 方案一localtime
    1. 优点仅使用C标准库缺点只能精确到秒级
  3. 方案二GetLocalTime sys  
    1. 优点能精确到毫秒级缺点使用了windows API
  4. 方案三systemtime
  5. 方案四timenull
  6. 方案五CTime
  7. 如何在C中将filetime时间转化为字符串
  8. filetime
  9. SYSTEMTIME st  
  10. char strTime128
  11. sprintfstrTimed-d-d  dddstwYearstwMonthstwDaystwHourstwMinutestwSecond 
  12. 如何在C中将filetime时间转化为常规时间格式

Windows下C/C++获取当前系统时间

【原文】http://blog.csdn.NET/dadalan/article/details/5771693

写软件时经常要用到获取系统时间显示到状态栏,这里在前人的基础上总结了一下几种方案。

方案一:localtime()

time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
 typedef long time_t;        /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年1月18日19时14分07秒。

函数time可以获取当前日历时间时间,time的定义:
 time_t time(time_t *)

time_t (typedef __int64  time_t )只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到tm结构,time.h中结构tm的原型是:

[cpp] view plaincopy
  1. struct tm {
  2. int tm_sec;     /* seconds after the minute - [0,59] */
  3. int tm_min;     /* minutes after the hour - [0,59] */
  4. int tm_hour;    /* hours since midnight - [0,23] */
  5. int tm_mday;    /* day of the month - [1,31] */
  6. int tm_mon;     /* months since January - [0,11] */
  7. int tm_year;    /* years since 1900 */
  8. int tm_wday;    /* days since Sunday - [0,6] */
  9. int tm_yday;    /* days since January 1 - [0,365] */
  10. int tm_isdst;   /* daylight savings time flag */
  11. };

可以看出,这个机构定义了年、月、日、时、分、秒、星期、当年中的某一天、夏令时。可以用这个结构很方便的显示时间。

用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间,函数原型:
 struct tm * localtime(const time_t *)
使用gmtime函数获取格林尼治时间,函数原型:
 struct tm * gmtime(const time_t *)

输出方式1:
[cpp] view plaincopy
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. void dsptime(const struct tm *); //输出时间。
  5. int main(void)
  6. {
  7. time_t nowtime;
  8. nowtime = time(NULL); //获取日历时间
  9. cout << nowtime << endl;  //输出nowtime
  10. struct tm *local,*gm;
  11. local=localtime(&nowtime);  //获取当前系统时间
  12. dsptime(local);
  13. gm=gmtime(&nowtime);  //获取格林尼治时间
  14. dsptime(gm);
  15. return 0;
  16. }
  17. void dsptime(const struct tm * ptm)
  18. {
  19. char *pxq[]={"日","一","二","三","四","五","六"};
  20. cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
  21. cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
  22. cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;
  23. }

输出方式2:

[cpp] view plaincopy
  1. #include <time.h>
  2. #include <stdio.h>
  3. int main( void )
  4. {
  5. time_t t = time(0);
  6. char tmp[64];
  7. strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
  8. puts( tmp );
  9. return 0;
  10. }

C/C++在time.h中提供了一个自定义时间格式的函数strftime,函数原型:
 size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
参数说明:
 char *strDest:用来存放格式化后的字符串缓存,
 size_t maxsize:指定最多可以输出的字符数,
 const char *format:格式化字符串,
 const struct tm *timeptr:要转换的时间。

可使用的格式化字符串:
%a 星期几的简写 
%A 星期几的全称 
%b 月分的简写 
%B 月份的全称 
%c 标准的日期的时间串 
%C 年份的后两位数字 
%d 十进制表示的每月的第几天 
%D 月/天/年 
%e 在两字符域中,十进制表示的每月的第几天 
%F 年-月-日 
%g 年份的后两位数字,使用基于周的年 
%G 年分,使用基于周的年 
%h 简写的月份名 
%H 24小时制的小时 
%I 12小时制的小时
%j 十进制表示的每年的第几天 
%m 十进制表示的月份 
%M 十时制表示的分钟数 
%n 新行符 
%p 本地的AM或PM的等价显示 
%r 12小时的时间 
%R 显示小时和分钟:hh:mm 
%S 十进制的秒数 
%t 水平制表符 
%T 显示时分秒:hh:mm:ss 
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年 
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53) 
%x 标准的日期串 
%X 标准的时间串 
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十进制年份 
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

方案二:GetLocalTime( &sys );

优点:能精确到毫秒级;缺点:使用了windows API

[cpp] view plaincopy
  1. #include <windows.h>
  2. #include <stdio.h>
  3. int main( void )
  4. {
  5. SYSTEMTIME sys;
  6. GetLocalTime( &sys );
  7. printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
  8. return 0;
  9. }

方案三:system("time");

优点:利用系统函数,还能修改系统时间

[cpp] view plaincopy
  1. //此文件必须是c++文件
  2. #include<stdlib.h>
  3. #include<iostream>
  4. using namespace std;
  5. void main()
  6. {
  7. system("time");
  8. }

方案四:time(null)

将当前时间折算为秒级,再通过相应的时间换算即可

[cpp] view plaincopy
  1. //此文件必须是c++文件
  2. #include<iostream>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7. time_t now_time;
  8. now_time = time(NULL);
  9. cout<<now_time;
  10. return 0;
  11. }

方案五:CTime

第一种:
[cpp] view plaincopy
  1. CString str; //获取系统时间   
  2. CTime tm; tm=CTime::GetCurrentTime();   
  3. str=tm.Format("%Y-%m-%d %H:%M:%S");  //主要是Y m d,H M S中间的连接符自己定义
  4. MessageBox(str,NULL,MB_OK);

第二种:

[cpp] view plaincopy
  1. SYSTEMTIME st;   
  2. CString strDate,strTime;   
  3. GetLocalTime(&st);   
  4. strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);   
  5. strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);

注释:

如何在C++中将filetime时间转化为字符串?

filetime

转化为systemtime

SYSTEMTIME st;  

char strTime[128];

sprintf(strTime,"%d-%d-%d  %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); 

参考:

如何在C++中将filetime时间转化为常规时间格式?

http://bbs.csdn.net/topics/310015002

Windows下C/C++获取当前系统时间相关推荐

  1. c语言 程序延时 校准,Windows下C语言API修改系统时间,附自动校准时间源码

    C语言Windows时钟的结构体如下: typedef struct _SYSTEMTIME { WORD wYear;              /*年*/ WORD wMonth;         ...

  2. Linux下用C获取当前系统时间

    #include   <time.h> time_t   time(time_t   calptr); 返回的是日历时间,即国际标准时间公元1970年1月1日00   :   00   : ...

  3. Windows下bat脚本获取格式化日期时间

    date各个字段的含义: %date:~0,4%  表示指针从左向右向右偏0位,然后从指针偏移到的位置开始提取4位字符,结果是2021(年字段数值) %date:~5,2%  表示指针从左向右偏移5位 ...

  4. C++获取当前系统时间

    在Linux环境下,C++获取当前系统时间的相关代码: #include <stdio.h> #include <stdlib.h> #include <time.h&g ...

  5. C++怎样获取当前系统时间?

    C++怎样获取当前系统时间? 1.调用cmd函数来获取当前时间 2.识别系统API来获取当前时间 3.直接获取系统时间 1.调用cmd函数来获取当前时间 这个模块并不是特别的麻烦,因为这种方法是直接使 ...

  6. c语言编程获取当前系统时间包含年,月,日,时,分,秒.,C语言获取系统时间的几种方式...

    核心提示:C语言中如何获取时间?精度如何?1使用time_ttime(time_t*timer)精确到秒2使用clock_tclock()得到的是CPU时间精确到1/CLOCKS_PER_SEC秒3计 ...

  7. Windows下用Python获取电脑显示器物理尺寸和PPI

    Windows下用Python获取电脑显示器物理尺寸和PPI 背景 PPI 和 DPI Python获取PPI 方法一(WMI) 方法二(WINREG) 方法三(WMI+WINREG) 总结 背景 最 ...

  8. Oracle——获取当前系统时间以及插入日期型数据(to_date的用法)

    获取当前系统时间 日期时间数据类型存储日期和时间值,包括年.月.日,小时.分钟.秒 主要的日期时间类型有: 1.DATE - 存储日期和时间部分,精确到整个的秒 语法 Select sysdate f ...

  9. java 当前时间戳_通过各种方法 获取当前系统时间、时间戳

    php中,如何通过各种方法 获取当前系统时间.时间戳,并备注各种格式的含义,可灵活变通. 1.获取当前时间方法date() 很简单,这就是获取时间的方法,格式为:date($format, $time ...

最新文章

  1. python jenkins库 api简介
  2. 实战Nginx与PHP(FastCGI)的安装、配置与优化
  3. 操作系统:多处理器编程-- 蒋炎岩老师
  4. linux clock命令,Linux中clock命令起什么作用呢?
  5. 电脑计算机的硬盘那些可以删除吗,我的电脑出现多个可移动磁盘该怎么删除?...
  6. 信息学奥赛一本通(1235:输出前k大的数)——堆排序
  7. jenkins搭建_如何搭建移动端自动化测试平台?没错,就用Jenkins!
  8. boost 单io_serverce 异步多线程资源保护代码
  9. ffmpeg将sdp转发_ffmpeg常用命令
  10. IDC机房对接阿里云
  11. 嵌入式操作系统内核原理和开发(头文件调整)
  12. java.lang.OutOfMemoryError: Java heap space异常
  13. 最专注和高效的查词法?网易有道词典笔2.0评测
  14. ABB RobotStudio6.08安装
  15. 间隙锁(Gap Lock)
  16. python 统计图绘制,Python绘制统计图表
  17. 08-A. 旅馆顾客统计(静态成员)
  18. esp32运行linux,ubuntu系统搭建ESP32 开发环境
  19. 创意休闲手游《急速感染》震撼来袭~
  20. 带bitlocker解密的pe_Win10使用BitLocker加密U盘|Win10自带BitLocker加密U盘

热门文章

  1. djangoORM语句
  2. oracle学习笔记(十八) PL/SQL 游标
  3. Servlet(自己实现的Servlet)细节
  4. 运送超级计算机 蓝书368
  5. Prim算法的3个版本
  6. CentOS编译安装PHP 7.0
  7. 部署不能产生class文件的问题
  8. SQL Server 中的事务和锁(三)-Range S-U,X-X 以及死锁
  9. HOJ 2278 IP Filtering (二分)
  10. 序列表转换成横向菜单