本文介绍了将字符串转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简单而简单。我有一个巨大的日期时间列表,如字符串:
Jun 1 2005 1:33 PM
1999年8月28日12:00 AM
我将把它们推回到正确的日期时间字段数据库,所以我需要把它们魔术变成真正的datetime对象。
任何帮助(即使只是一个正确的方向)将不胜感激。
编辑:这是通过Django的ORM,所以我不能使用SQL来插入转换。
解决方案
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33 PM','%b%d%Y%I:%M%p')
生成的 datetime
对象是timezone-naive。 p>
链接:
-
strptime的Python文档
:, -
Python文档
strftime
format mask:, -
注意:
-
strptime
=string parse time -
strftime
=string format time - 您将不必在6个月内再次搜索。
Short and simple. I've got a huge list of date-times like this as strings:
Jun 1 2005 1:33PM
Aug 28 1999 12:00AM
I'm going to be shoving these back into proper datetime fields in a database so I need to magic them into real datetime objects.
Any help (even if it's just a kick in the right direction) would be appreciated.
Edit: This is going through Django's ORM so I can't use SQL to do the conversion on insert.
解决方案
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
The resulting datetime
object is timezone-naive.
Links:
Notes:
strptime
= "string parse time"strftime
= "string format time"- Pronounce it out loud today & you won't have to search for it again in 6 months.
这篇关于将字符串转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!