C++ 获取系统时间

来源:互联网 发布:信用卡淘宝买东西套现 编辑:程序博客网 时间:2024/06/14 05:24

本文列举了网上常用的几种方法,仅供自己学习使用。

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

?
1
2
3
4
5
6
7
8
9
10
#include <time.h>
#include <stdio.h>
intmain( void)
{
  time_tt = time(0);
  chartmp[64];
  strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t));
  puts( tmp );
  return0;
}

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

?
1
2
3
4
5
6
7
8
9
#include <windows.h>
#include <stdio.h>
intmain( void)
{
 SYSTEMTIME sys;
 GetLocalTime(&sys);
 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);
 return0;
}

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

?
1
2
3
4
5
6
7
#include<stdlib.h>
#include<iostream>
usingnamespace std;
voidmain()
{
  system("time");
}

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

?
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<ctime>
usingnamespace std;
intmain()
{
 time_tnow_time;
 now_time = time(NULL);
 cout<<now_time;
 return0;
}

0 0
原创粉丝点击