将日期和时间的字符串转换为日期 [英] Convert a string with date and time to a date
本文介绍了将日期和时间的字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个日期时间字符串:
I've got this date time string:
post["date"] = "2007-07-18 10:03:19"
我想只提取2007-07-18日期。我看到一些参考 strptime
,但我不知道如何使用它。如何从这个字符串中提取日期?
I'd like to extract just "2007-07-18" as a date. I've seen some reference to strptime
but I'm not sure how to use it. How can I extract the date from this string?
推荐答案
其他两个答案是正确的,但如果你真的想要另外,您可以从datetime导入中使用 datetime
模块:
The other two answers are fine, but if you actually want the date for something else, you can use the datetime
module:
from datetime import datetime
d = datetime.strptime('2007-07-18 10:03:19', '%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')
现在可能是过度的,但是它会有用的。您可以查看所有格式说明符此处。
It might be overkill for now, but it'll come in useful. You can see all of the format specifiers here.
这篇关于将日期和时间的字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文