日付を文字列に変換

Pythonで扱う日付と時刻

Max Shron

Data Scientist and Author

ISO 8601 形式

from datetime import date

# 例の日付
d = date(2017, 11, 5)

# ISO 形式: YYYY-MM-DD print(d)
2017-11-05
# 日付を ISO 8601 形式で表し、リストに入れる
print( [d.isoformat()] )
['2017-11-05']
Pythonで扱う日付と時刻

ISO 8601 形式

# かつてコンピュータが苦手とした日付の例
some_dates = ['2000-01-01', '1999-12-31']

# 並べ替えて表示 print(sorted(some_dates))
['1999-12-31', '2000-01-01']
Pythonで扱う日付と時刻

その他の形式

d.strftime()
Pythonで扱う日付と時刻

その他の形式: strftime

# 例の日付
d = date(2017, 1, 5)

print(d.strftime("%Y"))
2017
# 文字列に追加のテキスト
print(d.strftime("Year is %Y"))
Year is 2017
Pythonで扱う日付と時刻

その他の形式: strftime

# 形式: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))
2017/01/05
Pythonで扱う日付と時刻

日付を文字列に変換

Pythonで扱う日付と時刻

Preparing Video For Download...