python - 如何将日期时间字符串转换回日期时间对象?

标签 python datetime

我正在数据库中存储一个 datetime 字符串。现在我面临一个问题。当我从数据库中获取字符串时,我需要将其转换回 datetime 对象...

有什么简单的方法吗?

日期时间字符串如下所示:

2010-11-13 10:33:54.227806

最佳答案

你想要 datetime.strptime(date_string, format)。

from datetime import datetime
datetime.strptime("2010-11-13 10:33:54.227806", "%Y-%m-%d %H:%M:%S.%f")

有关格式字符串的详细信息,请参阅 http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior

关于python - 如何将日期时间字符串转换回日期时间对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170655/

相关文章:

python - 从作为异步任务运行的函数中获取值

python - Pandas:向多索引表添加新行不起作用

Python 日期字符串 mm/dd/yyyy 转日期时间

python - 同时按日期和ID分组 Pandas 数据框

javascript - 从服务器获取哪种日期时间格式并传递到浏览器以用 Javascript 进行格式化?

mysql - 使用 groupby 将另一个表中的值插入到特定列中

python - 自动粘贴创建 -t plone3_buildout

python - 使用 python Popen 读取最后一行

R:如何获取时间序列数据中日期时间列的最大值

java - 在 Javascript 中转换 Java 公历