LoginSignup
2
5

More than 5 years have passed since last update.

[Python]明日の日付を取得する

Posted at

はじめに

研究室内からしか見れないスケジュールをslackに毎朝9時に通知する
上記の記事にあるようにスケジュールのリマインダーを作ったのですが,仕様変更を行うにあたって翌日の日付を取得したくなりました.
最初は「月末や年末,閏年の処理が面倒だな...」と思いながら条件式を書いていたのですが,簡単に取得できる方法があったのでメモ.

取得方法

timedeltaを使って1日加算してあげるだけで取得できます.
簡単.

tomorrow.py
import datetime

def main():
  # 今日の日付
  print('今日の日付')
  print('年: {0}'.format(datetime.datetime.now().year))
  print('月: {0}'.format(datetime.datetime.now().month))
  print('日: {0}'.format(datetime.datetime.now().day))

  # 明日の日付
  print('明日の日付')
  print('年: {0}'.format((datetime.datetime.now() + datetime.timedelta(days = 1)).year))
  print('月: {0}'.format((datetime.datetime.now() + datetime.timedelta(days = 1)).month))
  print('日: {0}'.format((datetime.datetime.now() + datetime.timedelta(days = 1)).day))


if __name__ == '__main__':
  main()

実行結果

result
$ python tomorrow.py
今日の日付
年: 2017
月: 11
日: 24
明日の日付
年: 2017
月: 11
日: 25

参考文献

How do I get tomorrow's date in Python?

2
5
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
2
5