1.时间戳 timestamp 是从1970年1月1日0时0分0秒开始的秒数
2.struct_time 包含9个元素的tuple
3.format time 已经格式化好便于阅读的时间
使用时间需要使用time模块
import time引入time模块
time.time()方法获取当前的时间,以timestamp的形式
>>>time.time()
1576372527.424447
time.localtime()方法:以struct_time的形式获取当前的当地时间
>>>time.localtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=14,
tm_hour=20, tm_min=15, tm_sec=49, tm_wday=5, tm_yday=348, tm_isdst=0)
time.gmtime()方法:以struct_time的形式获取当前的格林尼治时间
从struct_time中获取具体的年月日:
ctime.tm_year ctime.tm_mon .....
ttm_tm_isdst = 1来告知mktime()现在处于夏令时,明确使用ttm.tm_isdst = 0来告知未处于夏令时
不同时间表示法的转换
struct_time转timestamp: time.mktime(<struct_time>)
timestamp转struct_time: time.localtime(time.time())
这需求折腾了我半天..
import time
import datetime as datetime
def late_time(time2):
# 先获得时间数组格式的日期
#time2是外部传入的任意日期
now_time = datetime.datetime.strptime(time2, '%Y-%m-%d')
#如需求是当前时间则去掉函数参数改写 为datetime.datetime.now()
threeDayAgo = (now_time - datetime.timedelta(days =30))
# 转换为时间戳
timeStamp =int(time.mktime(threeDayAgo.timetuple()))
# 转换为其他字符串格式
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d")
return otherStyleTime
a = late_time("2019-3-30")
print(a)# 打印2018-02-28