0

各种程序根据其 Unix 平台 date 命令的语法输出日期格式。例如:Tue Nov 5 12:38:00 EST 2013

如何轻松地将其转换为 Python 日期对象?

4

1 回答 1

1

答案其实很简单。您只需要使用 datetime.strptime() 方法,它将日期的字符串表示(第一个参数)转换为基于指定字符串表示格式(第二个参数)的指令的日期对象。

在这种情况下,这是您将使用的代码:

import datetime

unix_date_format = '%a %b %d %H:%M:%S %Z %Y'
# Matches strings like Tue Nov 5 12:38:00 EST 2013

my_date = datetime.datetime.strptime(
    date_in_string_format, unix_date_format)

延伸阅读
datetime.strptime() 方法

于 2013-11-05T17:53:00.063 回答