python时间转换


>>> import datetime

string转datetime

>>> import datetime
>>> str = "2016-04-03"
>>> date_time = datetime.datetime.strptime(str,'%Y-%m-%d')
>>> date_time
datetime.datetime(2016, 4, 3, 0, 0)

datetime转string

>>> date_time.strftime('%Y-%m-%d')
'2016-04-03'

datetime转时间戳

>>> time_time = time.mktime(date_time.timetuple())
>>> time_time
1459612800.0

时间戳转string

>>> time.strftime('%Y-%m-%d',time.localtime(time_time))
'2016-04-03'
作者: honmaple
链接: https://honmaple.me/articles/2016/04/python时间转换.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论