Skip to main content
 首页 » 编程设计

python之如何在 Python 中将人类可读的日期和时间转换为 Unix 时间戳

2025年05月04日16leader

我有一堆人类可读的日期和时间(具体来说,是 nginx 日志中的默认格式),我希望将其转换为 Unix 时间戳。格式是这样的:

04/Dec/2013:18:56:05 +0000 

最可靠的方法是什么?是否有任何库可以用于此目的?

请您参考如下方法:

我想你在找datetime.strptime .

来自文档链接

The datetime.strptime() class method creates a datetime object from a string representing a date and time and a corresponding format string.

这样称呼

datetime.strptime(date_string, format)