strftime() で ISO 形式を再現する
前のチャプターでは、date オブジェクトから strftime() を使って文字列を作成しました。今度は datetime オブジェクトを使って、同様のことを練習していきます。
.strftime() を使って .isoformat() メソッドを再現し、データセット内で最初の乗車開始時刻を表示してください。
| Reference | |
|---|---|
| %Y | 4桁の年 (0000-9999) |
| %m | 2桁の月 (1-12) |
| %d | 2桁の日 (1-31) |
| %H | 2桁の時 (0-23) |
| %M | 2桁の分 (0-59) |
| %S | 2桁の秒 (0-59) |
この演習はコースの一部です
Pythonで扱う日付と時刻
演習の手順
- ISO 8601 の形式に合うように
fmtを完成させます。 .isoformat()と.strftime()の両方でfirst_startを表示します。結果は一致するはずです。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import datetime
from datetime import datetime
# Pull out the start of the first trip
first_start = onebike_datetimes[0]['start']
# Format to feed to strftime()
fmt = "____"
# Print out date with .isoformat(), then with .strftime() to compare
print(first_start.isoformat())
print(____)