1. 程式人生 > >·Python 獲取本地時間戳(包含毫秒)

·Python 獲取本地時間戳(包含毫秒)

Python 獲取本地時間戳(包含毫秒)

如何通過 Python 獲取一個完整的時間戳。

import time


def get_time_stamp():
    ct = time.time()
    local_time = time.localtime(ct)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (ct - int(ct)) * 1000
    time_stamp = "%s.%03d" % (data_head, data_secs)
    print(time_stamp)
    stamp = ("".join(time_stamp.split()[0].split("-"))+"".join(time_stamp.split()[1].split(":"))).replace('.', '')
    print(stamp)


if __name__ == '__main__':
    get_time_stamp()
輸出結果如下:
2018-04-17 15:28:28.434
20180417152828434
第一行為:年-月-日 時:分:秒 . 毫秒

第二行為拼接後時間戳