• Pyhon时间参数的应用


    Python获取 本周,上周,本月,上月,本季,上季,今年, 去年

    # -*- coding: utf-8 -*-
    # @time: 2019-05-13 17:30
    import datetime from datetime import timedelta now = datetime.datetime.now() # 今天 today = now # 昨天 yesterday = now - timedelta(days=1) # 明天 tomorrow = now + timedelta(days=1)

    # 当前季度 now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
    #本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6-now.weekday()) # 上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday()+7) last_week_end = now - timedelta(days=now.weekday()+1) # 本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) # 上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) # 本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1) # 上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) # 本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) # 去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1) last_year_start = datetime.datetime(last_year_end.year, 1, 1)

    关注公众号:

  • 相关阅读:
    Spark源码学习1.1——DAGScheduler.scala
    Spark随笔(三):straggler的产生原因
    Spark随笔(二):深入学习
    Spark随笔(一):Spark的综合认识
    Hadoop随笔(二):Hadoop V1到Hadoop V2的主要变化
    Hadoop随笔(一):工作流程的源码
    zookeeper 安装笔记 3.6.7
    OpenStack 与 大数据的融合
    三 概要模式 3) MR计数器计数 。无 reduce 计数
    五 数据组织模式 2) 分区模式 代码
  • 原文地址:https://www.cnblogs.com/QYGQH/p/10857941.html
Copyright © 2020-2023  润新知