首页 > 解决方案 > Datetime - Strftime and Strptime

问题描述

Date = datetime.datetime.today().strftime("%d %B %Y")
x = datetime.datetime.strptime(Date , "%d %B %Y")

returns:

2018-05-09 00:00:00

instead of: 9 May 2018, what am I doing wrong? Essentially I am trying to get the non zero padded version of strftime for which I searched around and I read that the way to go about it is strptime.

标签: pythondatetimestrptimestrftime

解决方案


一个可能的解决方案是使用str.lstrip

前任:

import datetime
Date = datetime.datetime.today().strftime("%d %B %Y")
print(Date.lstrip("0"))

输出:

9 May 2018

推荐阅读