date库:
获取时间戳:
timeStamp = int(time.time())
将时间戳转换为时间数组:
timeArr = time.local(timeStamp)
将时间数组转换为其他格式:
otherStyle = time.strftime(’%Y-%m-%d %H:%M:%S’,timeArr)
datetime库:
获取时间字符串:
timeString = datetime.datetime.now()
将时间戳转换为时间数组:
dateArr = datetime.datetime.utcfromtimestamp(stamp)
将时间字符串转换为其他格式:
otherStyle = dateArr.strftime(’%Y=%m=%d %H%M%S’)
"""
计算几天前并转换为指定格式。
"""
import datetime
import time
# 先获得时间数组格式的日期 三天前的时间数组
threeDayAgo = datetime.datetime.now() - datetime.timedelta(days=3)
print(threeDayAgo)
# 时间数组转换为任意指定格式
otherStyle = threeDayAgo.strftime('%Y--%m--%d %H::%M::%S')
print(otherStyle)
# 转换为时间戳
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
print(timeStamp)
# 将时间戳转换为时间数组
dateArr = datetime.datetime.utcfromtimestamp(timeStamp)
print(dateArr)
"""
将时间戳转换为指定格式日期
"""
# 给定一个时间戳,将其转换为指定格式的时间。
# 首先转换为时间数组,然后将时间数组转换为指定的时间格式
import time
now = int(time.time()) # 这是一个时间戳
print(now)
timeArray = time.localtime(now) # 时间数组
print(timeArray)
otherStyle = time.strftime('%Y-%m-%d %H:%M:%S',timeArray) # 指定格式
print(otherStyle)
stamp = 15525235387 # 时间戳
tArr = time.localtime(stamp)
print(tArr)
oStyle = time.strftime('%Y-%m-%d %H:%M:%S',tArr)
print(oStyle)
import datetime
now = datetime.datetime.now() # 这是一个时间字符串
print(now)
# 转换为指定格式
otherStyle = now.strftime('%Y=%m=%d %H\%M\%S')
print(otherStyle)
stamp = 15525235387 # 时间戳
# 转换为时间数组
dateArr = datetime.datetime.utcfromtimestamp(stamp)
print(dateArr)
# 将时间数组转换格式
otherStyle = dateArr.strftime('%Y=%m=%d %H\%M\%S')
print(otherStyle)