从Python中的日期字符串中删除时间戳 [英] Remove timestamp from date string in Python
本文介绍了从Python中的日期字符串中删除时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到的情况是我应该忽略日期字符串中的时间戳.我尝试了以下命令,但是没有运气.
I have situation where I should ignore the timestamp in a date string. I have tried the below command but with no luck.
"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
我的输出应该是:
2017年5月1日或2017年1月1日00:00
01MAY2017 or 01MAY2017 00:00
有人可以帮助我吗?
推荐答案
您的指令略有偏离,您需要捕获所有内容(无论您要保留什么内容).
Your directives are slightly off, and you need to capture all the contents (irrespective of what you want to retain).
start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M').date()
print start_date.strftime('%d%b%Y')
# '01May2017'
更新-在下面添加完整的代码:
Update - Adding complete code below:
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中的日期字符串中删除时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文