LoginSignup
0
0

More than 1 year has passed since last update.

[Python] よく使いそうな日付処理まとめ

Posted at

Pythonの日付処理をまとめておく。(必要に応じて追記)


from datetime import datetime,timedelta
now = datetime.utcnow() + timedelta(hours=9)

▼昨日

last_day = now + timedelta(days=-1)
print(last_day.strftime("%Y-%m-%d"))

▼先月1日

last_month = datetime(now.year, now.month, 1) + timedelta(days=-1)
print(last_month.strftime("%Y-%m-01"))

▼昨年同月1日

last_year_same_month = datetime(now.year-1, now.month, 1)
print(last_year_same_month.strftime("%Y-%m-01"))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0