简体   繁体   English

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

[英]How do I convert a human-readable date + time to a Unix timestamp in Python?

I have a bunch of human-readable dates and times (to be specific, the default format in nginx logs) that I wish to convert to Unix timestamps. 我有一堆人类可以理解的日期和时间(具体来说,是nginx日志中的默认格式),我希望将其转换为Unix时间戳。 The format is like this: 格式如下:

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

What's the most reliable way of doing this? 最可靠的方法是什么? Are there any libraries I can use for this purpose? 我可以为此目的使用任何库吗?

I think you are looking for datetime.strptime . 我认为您正在寻找datetime.strptime

From the doc link 从文档链接

The datetime.strptime() class method creates a datetime object from a string representing a date and time and a corresponding format string. datetime.strptime()类方法从代表日期和时间的字符串以及相应的格式字符串创建datetime对象。

Call it like 像这样称呼它

datetime.strptime(date_string, format)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM