码迷,mamicode.com
首页 > 编程语言 > 详细

python-6-1

时间:2018-05-10 00:06:24      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:time   strftime   函数   one   amp   return   oca   好的   orm   

1.定义一个时间戳转换成格式化时间的函数

import time
def timestamp_to_fomat(timestamp= None,format =‘%Y-%m-%d %H:%M:%S‘ ):
#1.默认返回当前格式化好的时间
#2.传入时间戳的话,把时间戳转换成格式化好的时间,返回
if timestamp:
time_tuple = time.localtime(timestamp)
res = time.strftime(format,time_tuple)
else:
res = time.strftime(format) #默认取当前时间
return res
2.
定义一个格式化好的时间转换成时间戳的函数:
def strToTimestamp(str=None,format=‘%Y%m%d%H%M%S‘):
if str: #如果传了时间的话
tp = time.strptime(str,format) #转成时间元祖
res = time.mktime(tp) #在转成时间戳
else:
res = time.time() #默认取当前的时间戳
return int(res)

python-6-1

标签:time   strftime   函数   one   amp   return   oca   好的   orm   

原文地址:https://www.cnblogs.com/happyna/p/9017234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!