9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python でN日前の日時を取得する

Posted at

Python でN日前の日時を取得する

pythonで日時を扱うためにはdatetimeモジュールを使う。
PHPなどでサポートされている2 days ago などの文字列からインスタンス化することはできないので、素直に計算して取得する。
日時の加算・減算を扱うためには timedeltaモジュールを使う。

>>> import datetime
>>> print (datetime.datetime.now())
2019-08-01 10:06:25.553554
>>> date = datetime.datetime.now() - datetime.timedelta(days=2)
>>> print(date)
2019-07-30 10:06:30.572633
9
3
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
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?