在某些情况下,我应该忽略日期字符串中的时间戳。我尝试了以下命令,但没有成功。
"start" variable used below is in AbsTime (Ex: 01MAY2017 11:45) and not a string.
start_date = datetime.datetime.strptime(start, '%d%^b%Y').date()
print start_date
我的输出应该是:
01MAY2017 or 01MAY2017 00:00
谁能帮帮我。
最佳答案
您的指令略有偏差,您需要捕获所有内容(无论您要保留什么)。
start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M').date()
print start_date.strftime('%d%b%Y')
# '01May2017'
更新 - 添加下面的完整代码:
import datetime
start = '01MAY2017 11:45'
start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M').date()
print start_date.strftime('%d%b%Y')
# 01May2017
关于python - 从 Python 中的日期字符串中删除时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316608/