どうも、ちょげ(@chogetarou)です。
DateTimeで年月日のみを文字列として取得する方法を紹介します。
方法

DateTimeで年月日のみを文字列として取得するには、strftime()を使います。
まず、対象のdatetimeからstrftime()を呼び出します。
そして、strftime()の引数に「’%Y-%m-%d’」を指定します。
day.strftime('%Y-%m-%d')
上記のstrftime()は、呼び出したdatetimeの年月日のみを文字列として取得します。
使用例
from datetime import datetime
current_day = datetime.now()
date = current_day.strftime('%Y-%m-%d')
print(date) #2022-06-29
print(type(date)) #<class 'str'>
コメント