python 字符串转datetime 时间戳转datetime 字符转转时间戳

# coding:utf8
import datetime

# 字符串转datetime
s = '2018-10-08 23:08:18'
dt = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
print dt

# datetime转字符串
s = dt.strftime('%Y%m%d')
print s

 

import datetime

timeStamp = 1540689500
dateArray = datetime.datetime.fromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print otherStyleTime  # 2018-10-28 09:18:20
import time

s = '2018-10-08 23:08:18'
time_stamp = int(time.mktime(time.strptime(s, "%Y-%m-%d %H:%M:%S")))
print time_stamp

 

你可能感兴趣的:(Python)