年月日時分秒を扱い、差分日時を取得する
任意の日付を作成するには datetime のコンストラクタに、年月日時分秒マイクロ秒を設定します。
ある日付からの差分を知るには timedelta クラスに適切な値を仕込んで datetime の変数と加減算します。 差分に指定できるのは、日、時、分、秒、ミリ秒、マイクロ秒、週、です。
年と月は指定できません。うるう年と、月に30日と月に31日が存在するから厳密に数学的に定義するのができないからでしょう。
なので timedelta クラスは、内部的には days, seconds, microseconds でしか管理しないという割り切った考え方になっています。
たとえばN年後が知りたければ、365 * N という値を timedelta の days に仕込んで下さい。厳密性をもとめないのならば datetime のコンストラクタの year の引数を N 年後の値をしこむだけでいいです。
考えてみれば、社会生活においては、1か月前とか1年後とか、結構テキトーにこういうフレーズを使っていますよね。
下記のコードがサンプルです。
import datetime
# 任意の日付を生成する.
y4 = 2022
m2 = 1
d2 = 23
hh = 12
mm = 34
ss = 56
us = 987654
dtm_now = datetime.datetime( y4, m2, d2, hh, mm, ss, us )
print( "==================================" )
dt = datetime.timedelta( days = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( days = +1 )
dtm_aft = dtm_now + dt
print( "days" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "----------------------------------" )
dt = datetime.timedelta( hours = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( hours = +1 )
dtm_aft = dtm_now + dt
print( "hours" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "----------------------------------" )
dt = datetime.timedelta( minutes = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( minutes = +1 )
dtm_aft = dtm_now + dt
print( "minutes" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "----------------------------------" )
dt = datetime.timedelta( seconds = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( seconds = +1 )
dtm_aft = dtm_now + dt
print( "seconds" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "----------------------------------" )
dt = datetime.timedelta( milliseconds = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( milliseconds = +1 )
dtm_aft = dtm_now + dt
print( "milliseconds" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "----------------------------------" )
dt = datetime.timedelta( microseconds = -1 )
dtm_bef = dtm_now + dt
dt = datetime.timedelta( microseconds = +1 )
dtm_aft = dtm_now + dt
print( "microseconds" )
print( "bef: {0}".format( dtm_bef ))
print( "now: {0}".format( dtm_now ))
print( "aft: {0}".format( dtm_aft ))
print( "==================================" )
# 任意の日付を生成する.
y4 = 2022
m2 = 1
d2 = 23
hh = 12
mm = 34
ss = 56
us = 0
dtm_now1 = datetime.datetime( y4, m2, d2, hh, mm, ss, us )
# 1日前の年月日時分秒を生成する.
d2 = 22
dtm_bef1 = datetime.datetime( y4, m2, d2, hh, mm, ss, us )
# 1日後の年月日時分秒を生成する.
d2 = 24
dtm_aft1 = datetime.datetime( y4, m2, d2, hh, mm, ss, us )
print( "bef: {0}".format( dtm_bef1 ))
print( "now: {0}".format( dtm_now1 ))
print( "aft: {0}".format( dtm_aft1 ))
print( "----------------------------------" )
# 差分を計算する.
delta_bef_now = dtm_bef1 - dtm_now1
delta_aft_now = dtm_aft1 - dtm_now1
print( delta_bef_now.days )
print( delta_bef_now.seconds )
print( delta_bef_now.microseconds )
print( delta_bef_now.total_seconds() ) # これはメソッド.
print( "----------------------------------" )
print( delta_aft_now.days )
print( delta_aft_now.seconds )
print( delta_aft_now.microseconds )
print( delta_aft_now.total_seconds() ) # これはメソッド.
print( "==================================" )
print( "finish." )
下記が実行結果です。
==================================
days
bef: 2022-01-22 12:34:56.987654
now: 2022-01-23 12:34:56.987654
aft: 2022-01-24 12:34:56.987654
----------------------------------
hours
bef: 2022-01-23 11:34:56.987654
now: 2022-01-23 12:34:56.987654
aft: 2022-01-23 13:34:56.987654
----------------------------------
minutes
bef: 2022-01-23 12:33:56.987654
now: 2022-01-23 12:34:56.987654
aft: 2022-01-23 12:35:56.987654
----------------------------------
seconds
bef: 2022-01-23 12:34:55.987654
now: 2022-01-23 12:34:56.987654
aft: 2022-01-23 12:34:57.987654
----------------------------------
milliseconds
bef: 2022-01-23 12:34:56.986654
now: 2022-01-23 12:34:56.987654
aft: 2022-01-23 12:34:56.988654
----------------------------------
microseconds
bef: 2022-01-23 12:34:56.987653
now: 2022-01-23 12:34:56.987654
aft: 2022-01-23 12:34:56.987655
==================================
bef: 2022-01-22 12:34:56
now: 2022-01-23 12:34:56
aft: 2022-01-24 12:34:56
----------------------------------
-1
0
0
-86400.0
----------------------------------
1
0
0
86400.0
==================================
finish.