python 获取今天、昨天、明天的时间时间戳

Bill78 等级 154 0 0
# 今天  最近24小时  最近一周  最近一月

if __name__ == '__main__':
    import datetime
    import time

    # 获取今天日期
    print("获取今天日期")
    today = datetime.date.today()
    print(today)  # 2019-09-05
    print(type(today))  # <type 'datetime.date'>
    print("**" * 10)

    # 昨天日期
    print("昨天日期")
    yesterday = today - datetime.timedelta(days=1)
    print(yesterday)  # 2019-09-04
    print("**" * 10)

    # 明天时间
    print("明天时间")
    tomorrow = today + datetime.timedelta(days=1)
    acquire = today + datetime.timedelta(days=2)
    print(tomorrow)  # 2019-09-06
    print(acquire)  # 2019-09-07
    print("**" * 10)

    # 昨天开始时间戳
    print("昨天开始时间戳")
    yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
    print(yesterday_start_time)  # 1567526400  转换后   2019-09-04 00:00:00
    print("**" * 10)

    # 昨天结束时间戳
    print("昨天结束时间戳")
    yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1
    print(yesterday_end_time)  # 1567612799  转换后   2019-09-04 23:59:59
    print("**" * 10)

    # 今天开始时间戳
    print("今天开始时间戳")
    today_start_time = yesterday_end_time + 1
    print(today_start_time)  # 1567612800  转换后   2019-09-05 00:00:00
    print("**" * 10)

    # 今天结束时间戳
    print("今天结束时间戳")
    today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
    print(today_end_time)  # 1567699199  转换后   2019-09-05 23:59:59
    print("**" * 10)

    # 明天开始时间戳
    print("明天开始时间戳")
    tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d')))
    print(tomorrow_start_time)  # 1567699200  转换后  2019-09-06 00:00:00
    print("**" * 10)

    # 明天结束时间戳
    print("明天结束时间戳")
    tomorrow_end_time = int(time.mktime(time.strptime(str(acquire), '%Y-%m-%d'))) - 1
    print(tomorrow_end_time)  # 1567785599  转换后  2019-09-06 23:59:59
    print("**" * 10)

    # 最近24小时
    print("最近24小时")
    time_now = time.time()
    before_24 = int(time_now - 24 * 60 * 60)
    print(before_24)
    print("**" * 10)

    # 最近一周
    print("最近一周")
    time_now = time.time()
    before_week = int(time_now - 7 * 24 * 60 * 60)
    print(before_week)
    print("**" * 10)


    # 最近一月
    print("最近一月")
    time_now = time.time()
    before_nonth = int(time_now - 30 * 24 * 60 * 60)
    print(before_nonth)
    print("**" * 10) 

本文转自 https://blog.csdn.net/a961634066/article/details/100554487,如有侵权,请联系删除。

预览图
收藏
评论区