如何在python中将字符串日期转换为datetime格式?

2024-04-20 03:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python中将datetime字符串转换为datetime格式,以便与其他日期进行比较?

string_date = "2013-09-28 20:30:55.78200"
abc = datetime.datetime.now()

if abc  > string_date :
    print True

Tags: 字符串truedatetimedatestringif格式now
2条回答

strptime的特定格式:

datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f")
#>>> datetime.datetime(2013, 9, 28, 20, 30, 55, 782000)

您应该使用^{}

import datetime

dt = datetime.datetime.strptime(string_date, fmt)

fmt必须是字符串的适当格式。你会找到reference on how to build your format here

相关问题 更多 >