0
0

More than 1 year has passed since last update.

ワンライナー年月日取得(python)

Posted at

巷を探してもいったん変数格納しているまどろっこしいのしか見つからないので、書いてみました。

今日の日付取得
import datetime
print(datetime.datetime.today().strftime('%Y-%m-%d'))

昨日の日付を取得するのにも、変数作りまくっているまどろっこしいのしかないですよね。次の式で一発取得です。

昨日の日付取得
import datetime
print((datetime.datetime.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d'))

取得したい日付はtimedeltaの引数を調節すればOKです。
使える引数は以下が参考になります。
https://docs.python.org/ja/3/library/datetime.html

良きワンライナーライフを!

0
0
2

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