C 獲取當前時間的方法

2021-10-21 17:40:36 字數 799 閱讀 9366

使用ctime類。但是直接用format好像無法獲取到毫秒級別,想獲取毫秒級別的請參考方法二。

ctime ctime = ctime::

getcurrenttime()

;cstring strcreatedate = ctime.

format(_t

("%y-%m-%d %h:%m:%s"))

;

最後輸出結果如下:

2021-03-10 19:08:11

參考:

ctime類官方文件

使用systemtime類。可以獲取到毫秒級別。

systemtime lpsystime;

getlocaltime

(&lpsystime)

;cstring strcreatedate;

strcreatedate.

format(_t

("%04d-%02d-%02d %02d:%02d:%02d %03d"

), lpsystime.wyear,

lpsystime.wmonth, lpsystime.wday, lpsystime.whour, lpsystime.wminute,

lpsystime.wsecond, lpsystime.wmilliseconds)

;

最後輸出結果如下:

2021-03-10 19:08:11 444

參考的博文

c 獲取當前時間

include include using namespace std int main time t ltime char tmpbuf 128 方法1 分別獲取當前時間,日期 display operating system style date and time.strtime tmpbuf ...

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...