c++ 如何獲取系統時間
//方案— 優點:僅使用C標準庫;缺點:只能精確到秒級
#include <time.h>
#include <stdio.h>
int main( void )
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
puts( tmp );
return 0;
}
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根據格式字串生成字串。
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:中國標準時間
%% % 字元。
//方案二 優點:能精確到毫秒級;缺點:使用了windows API
#include <windows.h>
#include <stdio.h>
int main( 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);
return 0;
}
//方案三,優點:利用系統函式,還能修改系統時間
//此檔案必須是c++
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}
//方案四,將當前時間折算為秒級,再通過相應的時間換算即可
//此檔案必須是c++檔案
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}
相關推薦
linux c獲取系統時間戳
#include<iostream> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ struc
C# 獲取系統時間及時間格式
--DateTime 數字型 System.DateTime currentTime=new System.DateTime(); 取當前年月日時分秒 currentTime=System.DateTime.Now; 取當前年 int 年=curren
Windows下C++獲取系統時間
使用GetLocalTime()函式 標頭檔案包含 Windows.h #include<Windows.h> SYSTEMTIME sysTime; GetLocalTime(&sysTime);
c++獲取系統時間精確到秒
#include "time.h" #include "stdio.h" #include "stdlib.h" #include <iostream> using namespace std; int main() {time_t now;struct tm *timenow;time(&
C++獲取系統時間的方式
轉自https://download.csdn.net/download/punomo/791219 //方案— 優點:僅使用C標準庫;缺點:只能精確到秒級 #include <time.h> #include <stdio.h> in
Windows下c++獲取系統時間的三種方法
1、CTime類的static介面GetCurrentTime() 包含的標頭檔案#include <atltime.h> CTime類的物件表示的時間是基於格林威治標準時間(GMT)的。 CTime表示的日期上限是3000年12月31日,下限是1970年1月1
C獲取系統時間的方法(linux下)
asctime(將時間和日期以字串格式表示) 相關函式 time,ctime,gmtime,localtime 表頭檔案 #include<time.h> 定義函式 char * asctime(const struct tm * timeptr
linux下C獲取系統時間的方法
asctime(將時間和日期以字串格式表示) 相關函式 time,ctime,gmtime,localtime 表頭檔案 #include<time.h> 定義函式 char * asctime(const struct tm * ti
【C獲取系統時間】C語言獲取系統時間的幾種方式
C 語言中如何獲取時間?精度如何? 1 使用 time_t time( time_t * timer ) 精確到秒 2 使用 clock_t clock() 得到的是 CPU 時間 精確到 1/CLOCKS_PER_SEC 秒 3 計算時間差使用 double difftime( time_t tim
c++/c獲取系統時間,並格式化輸出
話說最難消受美人恩,女士的要求是很難拒接的。。。 應一女士要求,幫其實現個小程式。要求c/c++獲取系統時間,並以中文格式化輸出。 第一反應,so簡單。 話說函式: 1、size_t strftime( char *strDest, size_t maxsize, const
C/C++ 獲取系統時間,系統延遲函式呼叫
C或C++呼叫Windows系統函式 實現延時 或 獲取當前時間的處理。可在VC++或MInGW上用 C或C++實現。 1、 實現延時: 標頭檔案: #include<windows.h> 函式: Sleep(int time); //time單位為ms,
Linux 環境下C/C++獲取系統時間 && 每隔500ms迴圈一次程式碼實現
環境:NetBeans IDE 8.2 + 遠端主機Linux 獲取當前系統時間getCurrentTime()程式碼如下: #include<sys/time.h> long getCurrentTime(){ struct timeval tv; gett
C# 獲取系統相關時間
獲取系統時鐘頻率 [DllImport("kernel32")] static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); 獲取系統時鐘計數 [DllImport("ker
C# 獲取系統當前時間 多格式
c#獲取當前日期時間 我們可以通過使用DataTime這個類來獲取當前的時間。通過呼叫類中的各種方法我們可以獲取不同的時間:如:日期(2008-09-04)、時間(12:12:12)、日期+時間(2008-09-04 12:11:10)等。 //獲取日期+時間 Dat
C語言獲取系統時間方法
需要利用C語言的時間函式time和localtime,具體說明如下: 一、函式介面介紹: 1、time函式。 形式為time_t time (time_t *__timer); 其中time_t為time.h定義的結構體,一般為長整型。 這個函式會獲取
C語言獲取系統時間的幾種方式
C語言中如何獲取時間?精度如何? 1 使用time_t time( time_t * timer ) 精確到秒 2 使用clock_t clock() 得到的是CPU時間 精確到1/CLOCKS_PER_SEC秒 3 計算時間差使用double difftime( time_t timer1, time_
C#/VB.net/VB 獲取系統時間
系統執行中的一個度量主線就是時間,因此時間的獲取可以說是很普遍的。而東西多了,難免就混亂。今天總結一下vb/vb.net/C#獲取系統時間的語法格式,方便日後查閱。 vb6.0
C語言獲取系統時間的幾種方式 !
四.設定計時器 定義TIMER ID #define TIMERID_JISUANFANGSHI 2 在適當的地方設定時鐘,需要開始其作用的地方; SetTimer(TIMERID_JISUANFANGSHI,200,NULL); 在不需要定時器的時候的時候銷燬掉時鐘 KillTimer(TIMERID_J
C++ 獲取系統當前時間方式
#include <time.h> //// 方式一 time_t tt = time(NULL); tm* t= localtime(&tt); printf("%d-%02d-%02d %02d:%02d:%02d\n", t->t
c++ 如何獲取系統時間
c++ 如何獲取系統時間 2008-04-28 15:34 //方案— 優點:僅使用C標準庫;缺點:只能精確到秒級 #include <time.h> #include <stdio.h> int main( void ) { time_t