1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonの日付処理:time.localtimeとdatetime.nowの使い分け、replaceとtimedeltaの違い

Last updated at Posted at 2025-08-18

はじめに

Pythonで日付や時刻を扱うときtime.localtime()datetime.now() のどちらを使うべきかで迷うことがありました。
さらに日付を操作する際には replace()timedelta という2つの手段がありそれぞれの使いどころが少し分かりにくいと感じました。

そこで今回、現在時刻の取得から日付操作までを整理し違いをまとめてみました。

現在時刻の取り方

1. time.localtime()

python
import time

current_time = time.localtime()
print(current_time.tm_year) # 年
print(current_time.tm_mon) # 月
print(current_time.tm_mday) # 日

# 文字列にしたいなら strftime を使う
print(time.strftime("%Y-%m-%d %H:%M:%S", current_time))
  • time.localtime() は現在のローカル時刻をstruct_timeオブジェクト(名前付きタプル風オブジェクト)で返す

  • struct_timeオブジェクトは数値の集まりに近い(演算機能はほぼなし)

    • time.struct_time(tm_year=2025, tm_mon=8, tm_mday=18,
      tm_hour=22, tm_min=30, tm_sec=5,
      tm_wday=0, tm_yday=230, tm_isdst=0)
  • .tm_year や .tm_hour のように指定するとintとして取り出せる

  • 「年だけ取り出す」「秒数を比較する」など、数値で扱いたいときに便利

  • 表示用途には strftime で文字列に整形して使う

2. datetime.now()

python
from datetime import datetime

now = datetime.now()
print(now)              # そのまま出力 2025-08-18 22:30:05.123456
print(now.strftime("%Y-%m-%d %H:%M:%S"))  # 好きな形式に整形 2025-08-18 22:30:05
print(now.strftime("%Y年%m月%d日 %H時%M分%S秒"))  # 2025年08月18日 22時30分05秒
  • datetime.now() は現在のローカル時刻をdatetimeオブジェクトで返す

  • datetimeオブジェクトは日付計算や文字列化など便利メソッドが揃っている

  • print(now) するだけで人間に読みやすい形式(ISO風の文字列)が出力される

    • 2025-08-18 22:30:05.123456
  • マイクロ秒まで含まれる

  • .strftime() で即フォーマットできる

  • 日時の計算・加工をしたいときに便利

  • 普段使いは datetime メインで良さそう

datetime の便利な機能

属性/メソッド 意味
year 2025
month 月 (1–12) 8
day 日 (1–31) 18
hour 時 (0–23) 22
minute 分 (0–59) 30
second 秒 (0–59) 5
microsecond マイクロ秒 123456
weekday() 曜日 (0=月曜) 0
strftime() 任意フォーマット化 "2025-08-18 22:30:05"
replace() 値を差し替える hour=0 など
timedelta併用 日時の加減算 翌日・数時間後の計算

日付の操作

Pythonで日付を操作するときは大きく分けて2つのアプローチがある。

  • replace:日付や時刻の一部を「強制的に置き換える」

  • timedelta:日付や時刻を「加算・減算する」

1. replace() ― 値を差し替える

replace は「計算」ではなく「値の上書き」。

python
from datetime import datetime

now = datetime(2025, 8, 18, 22, 15, 34)

# 今日の0時を作る
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
print(midnight)
# 2025-08-18 00:00:00

# 今月の1日を作る
first_day = now.replace(day=1)
print(first_day)
# 2025-08-01 22:15:34

# 同じ日付で会議の時刻を指定
meeting_time = now.replace(hour=15, minute=0)
print(meeting_time)
# 2025-08-18 15:00:00

特定のフィールドを固定したいときに使う

例:「その日の0時」「その月の1日」「今日の15時」

存在しない日付を入れるとエラーになる(例:1月31日→replace(day=32))

→ 翌日を出す用途には不向き

2. timedeltaクラス ― 日付や時間を進める/戻す

「翌日」「3時間前」などを扱うなら timedelta が王道。

timedelta は datetime モジュール内のクラスで、
timedelta(days=1) のように書くと コンストラクタを呼び出してインスタンス(時間差オブジェクト)を生成する。

python
from datetime import datetime, timedelta

now = datetime(2025, 1, 31, 20, 30, 0)

# 翌日
tomorrow = now + timedelta(days=1)
print(tomorrow)
# 2025-02-01 20:30:00

# 3時間前
three_hours_ago = now - timedelta(hours=3)
print(three_hours_ago)
# 2025-01-31 17:30:00

月末・年末をまたいでも自動で調整される

「翌日」「n日後」「◯時間後」を計算するのに最適

まとめ

現在時刻の取得

  • 数値で扱うなら → time.localtime()

  • 表示や汎用的な処理なら → datetime.now()

日付操作

  • 値を強制的に差し替えたい → replace()

  • 日付や時間を進めたい/戻したい → timedeltaクラス

おわりに

今回は「現在時刻の取り方」で迷ったのがきっかけでしたが、調べていくうちに日付操作の違いも整理できました。

仕組みを分けて考えると意外とシンプルでした。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?