本文共 1897 字,大约阅读时间需要 6 分钟。
-
| a = "2013-10-10 23:40:00" timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S" ) timeStamp = int (time.mktime(timeArray)) |
| 如a = "2013-10-10 23:40:00" ,想改为 a = "2013/10/10 23:40:00" timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S" ) otherStyleTime = time.strftime( "%Y/%m/%d %H:%M:%S" , timeArray) 利用localtime()转换为时间数组,然后格式化为需要的格式,如 timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime( "%Y-%m-%d %H:%M:%S" , timeArray) otherStyletime == "2013-10-10 23:40:00" dateArray = datetime.datetime.utcfromtimestamp(timeStamp) otherStyleTime = dateArray.strftime( "%Y-%m-%d %H:%M:%S" ) otherStyletime == "2013-10-10 23:40:00" now = int (time.time()) ->这是时间戳 转换为其他日期格式,如: "%Y-%m-%d %H:%M:%S" timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime( "%Y-%m-%d %H:%M:%S" , timeArray) now = datetime.datetime.now() ->这是时间数组格式 otherStyleTime = now.strftime( "%Y-%m-%d %H:%M:%S" ) threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3 )) timeStamp = int (time.mktime(threeDayAgo.timetuple())) otherStyleTime = threeDayAgo.strftime( "%Y-%m-%d %H:%M:%S" ) 注:timedelta()的参数有:days,hours,seconds,microseconds dateArray = datetime.datetime.utcfromtimestamp(timeStamp) threeDayAgo = dateArray - datetime.timedelta(days = 3 ) |
转载地址:http://lxlwo.baihongyu.com/