
>>> from datetime import datetime >>> datetime.now() datetime.datetime(2020, 3, 21, 22, 15, 12, 15172) # 最簡單的方式就這樣做 >>> str(datetime.now()) '2020-03-21 22:15:22.177988' >>> str(datetime.now())[-7:] '.125355' # 如果只要取到秒 >>> str(datetime.now())[:-7] '2020-03-21 22:15:50' # 使用 strftime 取到 micro-second >>> datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') '2020-03-21 22:16:27.581924' # 使用 strftime 取到 milli-second >>> datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] '2020-03-21 22:16:37.023'
參考資料:
Leave a Reply