LoginSignup
0
2

More than 3 years have passed since last update.

Python 3 で現在日時を取得する

Posted at

概要

  • Python 標準ライブラリである datetime モジュールを使用する
  • 日本のタイムゾーンを指定して現在日時情報を取得する

ソースコード

# datetime モジュールを読み込む
from datetime import datetime, timedelta, timezone

# タイムゾーンを指定して現在日時情報を取得する
now = datetime.now(timezone(timedelta(hours=9), 'JST'))

# 型特有の書式指定で文字列化する
str = '{0:%Y-%m-%d %A %H:%M:%S.%f %z (%Z)}'.format(now)

# 出力する
print(str)

実行結果例

2019-05-26 Sunday 10:59:53.718142 +0900 (JST)

今回の環境

$ python --version
Python 3.7.3

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.4
BuildVersion:   18E226

参考資料

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