0
0

More than 3 years have passed since last update.

Python、stftime、strptimeの違い

Last updated at Posted at 2020-03-12

stftimeと、strptimの違いについてメモ

(1)前提

datetimeモジュールのdatetiemクラスをインポートして、today()関数の結果を変数tに代入

from datetime import datetime
t = datetime.today
t

とすると、

datetime.datetime(2020, 3, 12, 23, 37, 45, 238787)

となる

(2)datetime型 → str型

str_t = t.strftime('%Y年%m月%d日/%H時%M分%S秒')
str_t

とすると、

'2020年03月12日/23時37分45秒'

となる

(3)str型 → datetime型 

time_t = datetime.strptime(str_t, '%Y年%m月%d日/%H時%M分%S秒')
time_t

とすると、

datetime.datetime(2020, 3, 12, 23, 37, 45)

となる。

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