作 ▸
Pythonのdatetimeで日時を扱う (取得, フォーマット, 計算)
Python 3.8.10
もくじ
現在日時をタイムスタンプで取得
import time
ts = time.time()
print(now)
# -> 1630696465.0573897
現在日時をdatetimeオブジェクトで取得
from datetime import datetime, timezone
now = datetime.now()
print(now)
# -> 2021-08-02 22:51:02.118206
from datetime import datetime, timezone
utcnow = datetime.now(timezone.utc)
print(utcnow)
# -> 2021-08-02 13:51:02.126749+00:00
任意の日時をdatetimeオブジェクトで取得
from datetime import datetime
dt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6)
print(dt)
# -> 2000-01-02 03:04:05.000006
from datetime import datetime, timezone
utcdt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6, tzinfo=timezone.utc)
print(utcdt)
# -> 2000-01-02 03:04:05.000006+00:00
from datetime import datetime
dt = datetime.fromtimestamp(946782245.000006)
print(dt)
# -> 2000-01-02 12:04:05.000006
from datetime import datetime, timezone
dt = datetime.fromtimestamp(946782245.000006, timezone.utc)
print(dt)
# -> 2000-01-02 03:04:05.000006+00:00
datetimeオブジェクトからいろいろ取得
from datetime import datetime, timezone
utcdt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6, tzinfo=timezone.utc)
dt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6)
# 年月日時分秒
print(utcdt.year) # -> 2000
print(utcdt.month) # -> 1
print(utcdt.day) # -> 2
print(utcdt.hour) # -> 3
print(utcdt.minute) # -> 4
print(utcdt.second) # -> 5
print(utcdt.microsecond) # -> 6
# タイムゾーン
print(utcdt.tzinfo) # -> datetime.timezone.utc
print(dt.tzinfo) # -> None
# 曜日
print(utcdt.weekday()) # -> 6 (月曜日=0, 日曜日=6)
print(utcdt.isoweekday()) # -> 7 (月曜日=1, 日曜日=7)
# タイムスタンプ
print(utcdt.timestamp()) # -> 946782245.000006
datetimeオブジェクト ⇔ 文字列 の変換
from datetime import datetime, timezone
utcdt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6, tzinfo=timezone.utc)
print(utcdt.strftime('%Y-%m-%d %H:%M:%S.%f %z'))
# -> '2000-01-02 03:04:05.000006 +0000'
print(utcdt.strftime('%Y%m%d%H%M%S'))
# -> '20000102030405'
from datetime import datetime
utcdt = datetime.strptime('2000-01-02 03:04:05.000006 +0000', '%Y-%m-%d %H:%M:%S.%f %z')
print(utcdt)
# -> 2000-01-02 03:04:05.000006+00:00
dt = datetime.strptime('2000-01-02 03:04:05.000006', '%Y-%m-%d %H:%M:%S.%f')
print(dt)
# -> 2000-01-02 03:04:05.000006
dt = datetime.strptime('20000102030405', '%Y%m%d%H%M%S')
print(dt)
# -> 2000-01-02 03:04:05
日時の比較
from datetime import datetime
dt1 = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6)
dt2 = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=7)
if dt1 < dt2:
print('dt2のほうが未来')
日時の計算
from datetime import datetime
dt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6)
# 10秒後
alter_dt = datetime.fromtimestamp(dt.timestamp() + 10)
print(dt) # -> 2000-01-02 03:04:05.000006
print(alter_dt) # -> 2000-01-02 03:04:15.000006
from datetime import datetime, timedelta
dt = datetime(year=2000, month=1, day=2, hour=3, minute=4, second=5, microsecond=6)
# 10秒後
alter_dt = dt + timedelta(seconds=10)
print(dt) # -> 2000-01-02 03:04:05.000006
print(alter_dt) # -> 2000-01-02 03:04:15.000006
# 1週間1日1時間1分1秒1ミリ秒1マイクロミリ秒後
alter_dt = dt + timedelta(weeks=1, days=1, hours=1, minutes=1, seconds=1, milliseconds=1, microseconds=1)
print(alter_dt) # -> 2000-01-10 04:05:06.001007