我遇到了有线问题。
from datetime import datetime, timedelta
start_date = '2019-05-01'
end_date = '2020-04-30'
start_date = datetime.strptime(start_date, "%Y-%m-%d")
print(start_date)
new_start_date = (datetime.strptime(end_date, '%Y-%m-%d') - timedelta(days=360)).strftime('%Y-%m-%d')
print(new_start_date)
回报是
2019-05-01 00:00:00
2019-05-06
看起来,第一个“ start_date”包含日期和时间,第二个“ new_start_date”仅包含日期。为什么?如何更改以使第一个“ start_date”仅返回日期,没有时间?
strptime
返回一个datetime
对象。文献资料
classmethod datetime.strptime(date_string,format)
返回对应于date_string的datetime,并根据格式进行解析。
其中,asstrftime
返回由格式化字符串指定的字符串。文献资料
date.strftime(格式)
返回表示日期的字符串,由明确的格式字符串控制。表示小时,分钟或秒的格式代码将显示0个值。有关格式设置指令的完整列表,请参见strftime()和strptime()行为。
在您的示例中;
datetime.strptime(start_date, "%Y-%m-%d") #2019-05-01 00:00:00
但是,如果您要使用它strftime
来格式化,它将继续减少时间。
datetime.strptime(start_date, "%Y-%m-%d").strftime('%Y-%m-%d')) #2019-05-01
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句