C 中如何獲取當前時間

時間 2021-09-13 17:53:47

1樓:匿名使用者

方案— 優點:僅使用c標準庫;缺點:只能精確到秒級#include

#include

int main( void )

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

#include

int main( void )

方案三,優點:利用系統函式,還能修改系統時間//此檔案必須是c++檔案

#include

#include

using namespace std;

void main()

方案四,將當前時間折算為秒級,再通過相應的時間換算即可//此檔案必須是c++檔案

#include

#include

using namespace std;

int main()

{time_t now_time;

now_time = time(null);

cout<

2樓:藍豆

#include

#include

#include

#include

#include

using namespace std;

class clock

void run(void) }}

sleep(1);}}

private:

int m_hour;

int m_min;

int m_sec;

};int main(void)

3樓:匿名使用者

getlocaltime ( )

如何用c語言獲取當前系統時間,如何用C語言獲取當前系統時間?

風若遠去何人留 需要利用c語言的時間函式time和localtime,具體說明如下 一 函式介面介紹 1 time函式。形式為time t time time t timer 其中time t為time.h定義的結構體,一般為長整型。這個函式會獲取當前時間,並返回。如果引數 timer非空,會儲存相...

如何獲取當前系統時間,如何獲取前一天的系統日期

通過php獲取系統前一天的日期,用到的工具,notepad 步驟如下 通過notepad 寫一個php函式另存為檔案。說明 的含義,就是先通過strtotime將當前時間減去一天轉化成unix時間戳,然後data函式是將unix時間戳轉化成年月日時分秒。2.瀏覽器中執行函式,輸出結果如下 注意事項 ...

C中怎麼插入當前系統時間,c 怎麼將系統時間加入到建立的檔名中

winfrom還是asp.net?如果是asp.net,那麼就要為textbox1做個onclick事件 textbox1.text datetime.now.tostring 羊咩咩 獲取日期 時間 datetime.now.tostring 2017 8 7 15 04 00 datetime....