Pythonは秒タイムスタンプとミリ秒タイムスタンプを取得


1.秒タイムスタンプとミリ秒タイムスタンプ、マイクロ秒タイムスタンプの取得
元のコード
import time
import datetime

t = time.time()

print (t)                           #       
print (int(t))                      #      
print (int(round(t * 1000)))        #       
print (int(round(t * 1000000)))     #       

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
1613134066.0561264
1613134066
1613134066056
1613134066056126

Process finished with exit code 0

2.現在の日付時刻の取得
元のコード
import time
import datetime
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')  #         ,       
print(dt)
print(dt_ms)

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
2021-02-12 20:55:43
2021-02-12 20:55:43.013557

Process finished with exit code 0

3.日付を秒タイムスタンプに変更
元のコード
import time
import datetime
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print (ts)

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
1514774430

Process finished with exit code 0

4.秒タイムスタンプを日付に変更
元のコード
import time
import datetime
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
print (ts)

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
2018-01-13 00:27:10
1515774430

Process finished with exit code 0

5.時間フォーマットを別の時間フォーマットに変換
元のコード
import time
import datetime
dt = '08/02/2019 01:00'
dt_new = datetime.datetime.strptime(dt, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
print(dt_new)

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
2019-08-02 01:00:00

Process finished with exit code 0

6.転構造体時間struct_time
元のコード
import time
import datetime
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S')  #         
ta_ms = time.localtime(1486188476) #       ,        int,       
print(ta_dt)
print(ta_ms)

戻り値
C:\Anaconda3\python.exe D:/Project4Program/Python/CreateTxtPhoneList/test1.py
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1)
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)

Process finished with exit code 0