LoginSignup
1

More than 5 years have passed since last update.

pythonで今週の日付を任意のフォーマットで求める

Last updated at Posted at 2015-05-01

週次なバッチ実行で今日から一週間分の日付とか欲しくなったので備忘録。

weekly_dates.py

from datetime import datetime, timedelta

# 1つづつ出力
for i in xrange(7):
        print (datetime.now()+timedelta(i)).strftime("%Y/%m/%d")

# リスト内包表記でリストに突っ込む
print [(datetime.now()+timedelta(i)).strftime("%Y/%m/%d") for i in xrange(7)]


実行結果はこんなん。


$ python weekly_dates.py
2015/05/01
2015/05/02
2015/05/03
2015/05/04
2015/05/05
2015/05/06
2015/05/07
['2015/05/01', '2015/05/02', '2015/05/03', '2015/05/04', '2015/05/05', '2015/05/06', '2015/05/07']


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
1