Pythonのstrptimeとstrftimeでdatetimeの日付・時間を文字列に変換

こんにちは!TodoONadaの土門(@daikidomon)です。

Pythonではdatetimeモジュールを使うことで日付や時間を扱うことが出来ます。

この日付・時間データと文字列を相互に変換できるのが、strptimeとstrftimeになります。

参考:strptimeについて

参考:strftimeについて

目次

strftimeとstrptimeの使い方と2つの違い

まずは2つの違いは、日付→文字列変換か?文字列→日付変換か?になります。

strftimeモジュールは日付型を文字列型に変換します

strptimeモジュールは文字列型を日付型に変換します。使い方は以下になります。

from datetime import datetime

#strftimeの使い方
datetimeオブジェクト.strftime(変換するフォーマット)

#strptimeの使い方
datetime.strptime(文字列,文字列のフォーマット)

サンプルは以下になります。

from datetime import datetime

date_one = datetime(2020,5,22,12)
date_string = 2020/5/22 12:00:00

print(date_one:, date_one)
# date_one: 2020-05-22 12:00:00

print(date_oneのタイプ:, type(date_one))
# date_oneのタイプ: <class 'datetime.datetime'>

print(date_one.strftime('%Y/%m/%d %H:%M:%S') :, \
    date_one.strftime('%Y/%m/%d %H:%M:%S'))
# date_one.strftime('%Y/%m/%d %H:%M:%S') : 2020/05/22 12:00:00

print(date_one.strftime('%Y/%m/%d %H:%M:%S')のタイプ:, \
    type(date_one.strftime('%Y/%m/%d %H:%M:%S')))
# date_one.strftime('%Y/%m/%d %H:%M:%S')のタイプ: <class 'str'>

print(date_string:, date_string)
# date_string: 2020/5/22 12:00:00

print(date_stringのタイプ:, type(date_string))
# date_stringのタイプ: <class 'str'>

print(datetime.strptime(date_string,'%Y/%m/%d %H:%M') :, \
    datetime.strptime(date_string,'%Y/%m/%d %H:%M:%S'))
# datetime.strptime(date_string,'%Y/%m/%d %H:%M') : 2020-05-22 12:00:00

print(datetime.strptime(date_string,'%Y/%m/%d %H:%M')のタイプ:, \
    type(datetime.strptime(date_string,'%Y/%m/%d %H:%M:%S')))
# datetime.strptime(date_string,'%Y/%m/%d %H:%M')のタイプ: <class 'datetime.datetime'>

strftimeで日付・時間から文字列へ変換

datetimeで定義した日付・時間を文字列に変換します。

from datetime import datetime

dt = datetime()

dt.strftime(変換フォーマット)
フォーマット意味
%Y年(4桁)の取得
%y年(下2桁)の取得
%m月 (10 進表記)の取得 [01, 12]
%B月 (月名)の取得
%b月 (短縮月名)の取得
%d日 (数字)の取得[01, 31]
%H時刻(時)の取得。範囲[00, 23]
%I時刻(時)の取得。範囲[00, 12]
%p午前/午後の表示[AM, PM]
%M時刻(分)の取得[00, 59]
%S時刻(秒)の取得[00, 61]
%A曜日名取得
%a曜日名(短縮)取得
%w曜日(10 進表記)を取得[0 (日曜日), 6]
%U何週目かを取得(日曜初め) [00, 53]
%W何週目かを取得(月曜初め) [00, 53]
%x日付を適切な形式で取得
%X時間を適切な形式で取得
%j年中の日にちを取得 [001, 366]
%c日時を適切な形式で取得
%Y/%m/%d %H:%M:%S年/月/日 時:分:秒で時間を取得

サンプルは以下になります。

from datetime import datetime 

dt_one = datetime(2020, 5, 22, 12, 13, 14)

print(dt_one.strftime('%Y/%m/%d %H:%M:%S'):,  dt_one.strftime('%Y/%m/%d %H:%M:%S'))
# dt_one.strftime('%Y/%m/%d %H:%M:%S'): 2020/05/22 12:13:14

print(dt_one.strftime('%Y/%m/%d %H:%M:%S')のタイプ:,  type(dt_one.strftime('%Y/%m/%d %H:%M:%S')))
# dt_one.strftime('%Y/%m/%d %H:%M:%S')のタイプ: <class 'str'>

print(dt_one.strftime('%Y'):, dt_one.strftime('%Y'))
# dt_one.strftime('%Y'): 2020

print(dt_one.strftime('%y'):, dt_one.strftime('%y'))
# dt_one.strftime('%y'): 20

print(dt_one.strftime('%m'):, dt_one.strftime('%m'))
# dt_one.strftime('%m'): 05

print(dt_one.strftime('%B'):, dt_one.strftime('%B'))
# dt_one.strftime('%B'): May

print(dt_one.strftime('%b'):, dt_one.strftime('%b'))
# dt_one.strftime('%b'): May

print(dt_one.strftime('%d'):, dt_one.strftime('%d'))
# dt_one.strftime('%d'): 22

print(dt_one.strftime('%H'):, dt_one.strftime('%H'))
# dt_one.strftime('%H'): 12

print(dt_one.strftime('%I'):, dt_one.strftime('%I'))
# dt_one.strftime('%I'): 12

print(dt_one.strftime('%p'):, dt_one.strftime('%p'))
# dt_one.strftime('%p'): PM

print(dt_one.strftime('%M'):, dt_one.strftime('%M'))
# dt_one.strftime('%M'): 13

print(dt_one.strftime('%S'):, dt_one.strftime('%S'))
# dt_one.strftime('%S'): 14

print(dt_one.strftime('%A'):, dt_one.strftime('%A'))
# dt_one.strftime('%A'): Friday

print(dt_one.strftime('%a'):, dt_one.strftime('%a'))
# dt_one.strftime('%a'): Fri

print(dt_one.strftime('%w'):, dt_one.strftime('%w'))
# dt_one.strftime('%w'): 5

print(dt_one.strftime('%U'):, dt_one.strftime('%U'))
# dt_one.strftime('%U'): 20

print(dt_one.strftime('%W'):, dt_one.strftime('%W'))
# dt_one.strftime('%W'): 20

print(dt_one.strftime('%x'):, dt_one.strftime('%x'))
# dt_one.strftime('%x'): 05/22/20

print(dt_one.strftime('%X'):, dt_one.strftime('%X'))
# dt_one.strftime('%X'): 12:13:14

print(dt_one.strftime('%j'):, dt_one.strftime('%j'))
# dt_one.strftime('%j'): 143

print(dt_one.strftime('%c'):, dt_one.strftime('%c'))
# dt_one.strftime('%c'): Fri May 22 12:13:14 2020

strptimeで文字列から日付・時間へ変換

時間を表している文字列を上のフォーマット表で文字列のフォーマットを表し変換させます。

datetime.strftime(文字列,文字列のフォーマット)
フォーマット意味
%Y
%m
%d
%H
%M
%S

サンプルは以下になります。

from datetime import datetime

stringformat_one = 2020/05/23 12:13:14
stringformat_two = 2020年05月23日 12時13分14秒
stringformat_three = 05/23/2020 12:13:14

print(stringformat_one:, stringformat_one)
# stringformat_one: 2020/05/23 12:13:14

print(stringformat_oneのタイプ:, type(stringformat_one))
# stringformat_oneのタイプ: 2020/05/23 12:13:14

print(datetime.strptime(stringformat_one, '%Y/%m/%d %H:%M:%S'):, \
      datetime.strptime(stringformat_one, %Y/%m/%d %H:%M:%S))
# datetime.strptime(stringformat_one, '%Y/%m/%d %H:%M:%S'): 2020-05-23 12:13:14

print(datetime.strptime(stringformat_one,'%Y/%m/%d %H:%M:%S')のタイプ:, \
      type(datetime.strptime(stringformat_one, %Y/%m/%d %H:%M:%S)))
# datetime.strptime(stringformat_one,'%Y/%m/%d %H:%M:%S')のタイプ: <class 'datetime.datetime'>

print(stringformat_two:, stringformat_two)
# stringformat_two: 2020年05月23日 12時13分14秒

print(stringformat_twoのタイプ:, type(stringformat_two))
# stringformat_twoのタイプ: 2020年05月23日 12時13分14秒

print(datetime.strptime(stringformat_two,'%Y年%m月%d日 %H時%M分%S秒'):, \
      datetime.strptime(stringformat_two, %Y年%m月%d日 %H時%M分%S秒))
# datetime.strptime(stringformat_two,'%Y年%m月%d日 %H時%M分%S秒'): 2020-05-23 12:13:14

print(datetime.strptime(stringformat_two,'%Y年%m月%d日 %H時%M分%S秒')のタイプ:, \
      datetime.strptime(stringformat_two, %Y年%m月%d日 %H時%M分%S秒))
# datetime.strptime(stringformat_two,'%Y年%m月%d日 %H時%M分%S秒')のタイプ: 2020-05-23 12:13:14

print(stringformat_three:,stringformat_three)
# stringformat_three: 05/23/2020 12:13:14

print(stringformat_threeのタイプ:,type(stringformat_three))
# stringformat_threeのタイプ: 05/23/2020 12:13:14

print(datetime.strptime(stringformat_three,'%m/%d/%Y %H:%M:%S'):, \
      datetime.strptime(stringformat_three, %m/%d/%Y %H:%M:%S))
# datetime.strptime(stringformat_three,'%m/%d/%Y %H:%M:%S'): 2020-05-23 12:13:14

print(datetime.strptime(stringformat_three, '%m/%d/%Y %H:%M:%S')のタイプ:,\
      datetime.strptime(stringformat_three, %m/%d/%Y %H:%M:%S))
# datetime.strptime(stringformat_three,'%m/%d/%Y %H:%M:%S')のタイプ: 2020-05-23 12:13:14
スラスラ読める Pythonふりがなプログラミング (ふりがなプログラミングシリーズ)
よかったらシェアしてね!
  • URLをコピーしました!
目次