骑骡子赶猪  
# timeStamp = 1381419600
# timeArray = time.localtime(timeStamp)
# otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
# print otherStyleTime # 2013--10--10 23:40:00
# # 使用datetime
# timeStamp = 1381419600
# dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
# otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
# print otherStyleTime # 2013--10--10 15:40:00



# 时间戳转换为格式化
import time
timeStamp = 1570695095
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print(otherStyleTime)
# 格式化转换成时间戳
import time
def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):
time_tuple = time.strptime(str_time, format) # 把格式化好的时间 转换成元祖
result = time.mktime(time_tuple) # 把时间元祖转换成时间戳
return int(result)
print(str_to_timestamp('2019-09-21 00:00:00'))
 
# 将时间转换成秒
def t2s(t):
h, m, s = t.strip().split(":")
return int(h) * 3600 + int(m) * 60 + int(s)
print(t2s('10:35:50'))
 
获取昨天日期
import datetime
t = datetime.date.today()
t2 = datetime.timedelta(-1)
print(t)
print(t2)
print(t+t2)
 
获取当前日期时间
import datetime
import time
print(time.strftime('%Y%m%d %H:%M:%S',time.localtime(time.time())))
 
获取当天凌晨
import datetime
import time
today = datetime.date.today()
print(today)
today_time = int(time.mktime(today.timetuple()))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(today_time)))
 
 
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 获取今天零点
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,microseconds=now.microsecond)
# 获取23:59:59
lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
print(lastToday)
posted on 2019-04-18 15:34  骑骡子赶猪  阅读(96)  评论(0编辑  收藏  举报