LoginSignup
0
0

Pythonで「日付や時刻の一部の値を別の値に置き換えたインスタンスを生成する」の動作を確認してみた

Posted at

概要

Pythonで「日付や時刻の一部の値を別の値に置き換えたインスタンスを生成する」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
import datetime
tokyo_tz = datetime.timezone(datetime.timedelta(hours=9))
dt = datetime.datetime.now()
print(dt);
print(dt.replace(hour=4, minute=0, second=0))
print(dt.replace(year=2018, month=4, day=1))
print(dt.replace(tzinfo=tokyo_tz))

以下のコマンドを実行しました。

$ python3 sample.py 
2024-04-03 01:51:34.871159
2024-04-03 04:00:00.871159
2018-04-01 01:51:34.871159
2024-04-03 01:51:34.871159+09:00

まとめ

何かの役に立てばと。

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