1# 今天 最近24小时 最近一周 最近一月 2 3if __name__ == '__main__': 4 import datetime 5 import time 6 7 # 获取今天日期 8 print("获取今天日期") 9 today = datetime.date.today() 10 print(today) # 2019-09-05 11 print(type(today)) # <type 'datetime.date'> 12 print("**" * 10) 13 14 # 昨天日期 15 print("昨天日期") 16 yesterday = today - datetime.timedelta(days=1) 17 print(yesterday) # 2019-09-04 18 print("**" * 10) 19 20 # 明天时间 21 print("明天时间") 22 tomorrow = today + datetime.timedelta(days=1) 23 acquire = today + datetime.timedelta(days=2) 24 print(tomorrow) # 2019-09-06 25 print(acquire) # 2019-09-07 26 print("**" * 10) 27 28 # 昨天开始时间戳 29 print("昨天开始时间戳") 30 yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d'))) 31 print(yesterday_start_time) # 1567526400 转换后 2019-09-04 00:00:00 32 print("**" * 10) 33 34 # 昨天结束时间戳 35 print("昨天结束时间戳") 36 yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1 37 print(yesterday_end_time) # 1567612799 转换后 2019-09-04 23:59:59 38 print("**" * 10) 39 40 # 今天开始时间戳 41 print("今天开始时间戳") 42 today_start_time = yesterday_end_time + 1 43 print(today_start_time) # 1567612800 转换后 2019-09-05 00:00:00 44 print("**" * 10) 45 46 # 今天结束时间戳 47 print("今天结束时间戳") 48 today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1 49 print(today_end_time) # 1567699199 转换后 2019-09-05 23:59:59 50 print("**" * 10) 51 52 # 明天开始时间戳 53 print("明天开始时间戳") 54 tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) 55 print(tomorrow_start_time) # 1567699200 转换后 2019-09-06 00:00:00 56 print("**" * 10) 57 58 # 明天结束时间戳 59 print("明天结束时间戳") 60 tomorrow_end_time = int(time.mktime(time.strptime(str(acquire), '%Y-%m-%d'))) - 1 61 print(tomorrow_end_time) # 1567785599 转换后 2019-09-06 23:59:59 62 print("**" * 10) 63 64 # 最近24小时 65 print("最近24小时") 66 time_now = time.time() 67 before_24 = int(time_now - 24 * 60 * 60) 68 print(before_24) 69 print("**" * 10) 70 71 # 最近一周 72 print("最近一周") 73 time_now = time.time() 74 before_week = int(time_now - 7 * 24 * 60 * 60) 75 print(before_week) 76 print("**" * 10) 77 78 79 # 最近一月 80 print("最近一月") 81 time_now = time.time() 82 before_nonth = int(time_now - 30 * 24 * 60 * 60) 83 print(before_nonth) 84 print("**" * 10)
本文转自 https://blog.csdn.net/a961634066/article/details/100554487,如有侵权,请联系删除。
