zoukankan      html  css  js  c++  java
  • python 获取今天、昨天、明天的时间时间戳

    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/java/article/details/100554487

  • 相关阅读:
    假如我国国民生产总值的年增长率为7%, 计算10年后我国国民生产总值与现在相比增长多少百分比。计算公式为$p = (1+r)^n$ ,其中r为年增长率,n为年数,p为与现在相比的倍数
    不定积分40例
    docker容器
    Kubernetes搭建
    windows提权之mimikatz
    NodeJS沙箱逃逸&&vm
    jwt攻击手段
    yii2邮件配置教程,报Expected response code 250 but got code "553"原因
    git 撤销,放弃本地修改
    动态规划(含最短路径和正则匹配例子)
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/12651135.html
Copyright © 2011-2022 走看看