【引用】C/C++获取当前系统时间

01 //方案— 优点:仅使用C标准库;缺点:只能精确到秒级  

02 #include <time.h>   

03 #include <stdio.h>   

04 int main( void )   

05 {   

06     time_t t = time(0);   

07     char tmp[64];   

08     strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );   

09     puts( tmp );   

10     return 0;   

11 }

 

显示代码打印1 size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);


根据格式字符串生成字符串。


显示代码打印1 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:中国标准时间 
%% % 字符。
 


显示代码打印01 //方案二 优点:能精确到毫秒级;缺点:使用了windows API   

02 #include <windows.h>   

03 #include <stdio.h>   

04 int main( void )   

05 {   

06  SYSTEMTIME sys;   

07  GetLocalTime( &sys );   

08  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);   

09   return 0;  

10 }

 

显示代码打印1 //方案三,优点:利用系统函数,还能修改系统时间  

2 //此文件必须是c++文件  

3 #include<stdlib.h>  

4 #include<iostream>  

5 using namespace std;  

6 void main()  

7 {  

8     system("time");  

9 }

 

 

显示代码打印01 //方案四,将当前时间折算为秒级,再通过相应的时间换算即可  

02 //此文件必须是c++文件  

03 #include<iostream>  

04 #include<ctime>  

05 using namespace std;  

06 int main()  

07 {  

08  time_t now_time;  

09  now_time = time(NULL);  

10  cout<<now_time;  

11  return 0;  

12 }


文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/c++/cppsl/2008222/100445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值