Python中,时间字符串与时间戳的互相转换

时间字符串转换为时间戳:

import time
timestring = '2012-05-15 17:12:37'
timestamp = int(time.mktime(time.strptime(timestring, '%Y-%m-%d %H:%M:%S')))
print timestamp

时间戳转换为时间字符串:

import time
struct_time = time.localtime(1337073342)
timestring = time.strftime('%Y-%m-%d %H:%M:%S',struct_time)
print timestring
Previous Post Next Post

One thought on “Python中,时间字符串与时间戳的互相转换

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注