Python常用time处理
#coding=utf-8 # python2.7工具语句 from sys import version_info import time import datetime if version_info<(3,0): print('注意当前是:python2') else: print('python3') #把datetime转成字符串 def unixtime_toString(timeStamp='1381419600'): timeStamp =int(str(timeStamp)[:10]) return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp)) unixtime_toString('1530859253000') #把字符串转成datetime def string_toDatetime(string): return datetime.strptime(string, "%Y-%m-%d %H:%M:%S") #把字符串转成时间戳形式 def string_toTimestamp(strTime): return int(time.mktime(time.strptime(strTime, "%Y-%m-%d %H:%M:%S"))) #把时间戳转成字符串形式 def timestamp_toString(stamp): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp)) #把datetime类型转外时间戳形式 def datetime_toTimestamp(dateTim): return time.mktime(dateTim.timetuple()) import datetime #python 获取当前时间及前一天时间 print ((datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")) #python datetime获取几分钟、小时、天之前的时间 print ((datetime.datetime.now()-datetime.timedelta(minutes=2)).strftime("%Y-%m-%d %H:%M:%S")) #可以控制days、seconds、minutes、hours、weeks等
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#现在