C 獲取時間

2021-09-07 15:13:47 字數 1286 閱讀 4364

c++ 獲取時間

標頭檔案 chrono, 命名空間 std.

現在時間

std::chrono::system_clock::now() 返回系統時鐘的當前時間

時鐘std::chrono::system_clock 代表系統當前的時間, 是不穩定的時鐘, 並且提供了函式可將時間點轉化為 time_t 型別的值

std::chrono::steady_clock 表示乙個穩定的時鐘, 所謂穩定指呼叫 now()時, 其值總是大於上1次.

延遲std::this_thread::sleep_for() 和 std::this_thread::sleep_until()

std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 等待 100ms

1

#define _crt_secure_no_warnings

23 #include 4 #include

5 #include 6 #include 7 #include 8

9 std::string

gettimestr()10;

14 localtime_s(tt);

15std::stringstream stm;

16 stm << std::setfill('0'

);17 stm << std::setw(4) << (ltm.tm_year + 1900) << "-"

;18 stm << std::setw(2) << (ltm.tm_mon + 1) << "-"

;19 stm << std::setw(2) << (ltm.tm_mday) << "";

20 stm << std::setw(2) << (ltm.tm_hour) << "-"

;21 stm << std::setw(2) << (ltm.tm_min) << "-"

;22 stm << std::setw(2) <

2324

return

stm.str();25}

2627

void mytest(void)28

3940

int main(int argc, char * ar**, char *envp)

41

執行結果:

C 獲取時間

獲取日期 時間 datetime.now.tostring 2008 9 4 20 02 10 datetime.now.tolocaltime tostring 2008 9 4 20 12 12 獲取日期 datetime.now.tolongdatestring tostring 2008年9...

C 獲取時間

獲取日期 時間 datetime.now.tostring 2008 9 4 20 02 10 datetime.now.tolocaltime tostring 2008 9 4 20 12 12 獲取日期 datetime.now.tolongdatestring tostring 2008年9...

C 獲取時間

datetime 數字型 system.datetime currenttime new system.datetime 1.1 取當前年月日時分秒 currenttime system.datetime.now 1.2 取當前年 int 年 currenttime.year 1.3 取當前月 in...