LoginSignup
10
10

More than 5 years have passed since last update.

pythonで日付計算する方法

Posted at

こんにちはsekitakaです。

pythonのdatetimeモジュールのtimedeltaオブジェクトを使うと日時の加減算や、2つのdatetimeの差を求める計算を簡単に行う事ができます。

使用例

# coding:utf-8
# !/usr/bin/python
from datetime import datetime,timedelta
import pytz

# 現在時刻を元に加減算する
timezone = pytz.timezone('Asia/Tokyo')
now = datetime.now(tz=timezone)
delta = timedelta(days=+1)
tomorrow = now + delta
print "今は{0}".format(now)
print "今の1日後は{0}".format(tomorrow)

# 2つの日付の差を算出する
delta = tomorrow - now
print "差は{0}日".format(delta.days)

より詳細な使い方は公式ドキュメントを参考にしてください。

10
10
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
10
10