python 字符串时间转成时间戳
原创
©著作权归作者所有:来自51CTO博客作者littlehaes的原创作品,请联系作者获取转载授权,否则将追究法律责任
示例一
import time
t = "2020-10-31 12:44:27"
# 将字符串形式的时间转换为时间元组
t = time.strptime(t, "%Y-%m-%d %H:%M:%S")
# 将时间元组转换为时间戳
t = time.mktime(t)
# 1604119467.0
print(t)
示例二
import time
t = "Mon Mar 16 00:09:00 +0000 2015"
tt = time.strptime(t, "%a %b %d %H:%M:%S %z %Y")
# 将时间元组转换为时间戳
tt = time.mktime(t)
# 1426435740.0
print(t)
strftime()中各个字母的含义
年 %Y Year with century as a decimal number.
月 %m Month as a decimal number [01,12].
日 %d Day of the month as a decimal number [01,31].
小时 %H Hour (24-hour clock) as a decimal number [00,23].
分钟 %M Minute as a decimal number [00,59].
秒 %S Second as a decimal number [00,61].
时区 %z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
小时(12小时制)%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.