zoukankan      html  css  js  c++  java
  • Python之计算当前月份的日期范围(calendar、datetime)

    在当前月份中循环每一天
    大致的思路就是先计算出当月的第一天和下个月的第一天日期,还有当月总共有多少天,然后把第一天日期按照月总天数累加到下个月的第一天,就ok 啦
    from datetime import datetime, date, timedelta
    import calendar
    
    
    def get_month_range(start_date=None):
        if start_date is None:
            print(date.today())  # 2019-03-05  # 今天的日期
            start_date = date.today().replace(day=1)  # 修改当前时间。比如修改成当月1号
            print(start_date)  # 2019-03-01  当月的第一天日期
        _, days_in_month = calendar.monthrange(start_date.year, start_date.month)
        print(days_in_month)  # 31   当月总天数31天
        end_date = start_date + timedelta(days=days_in_month)
        print(end_date)  # 2019-04-01
        return (start_date, end_date)
    
    
    start_date,end_date = get_month_range()
    print(start_date,end_date)  #日期范围 2019-03-01 2019-04-01
    a_day=timedelta(days=1)
    print(a_day)  # 1 day, 0:00:00
    li=[]
    while start_date<end_date:
        li.append(start_date)
        start_date+=a_day
    # print(li)
    # [datetime.date(2019, 3, 1), datetime.date(2019, 3, 2), datetime.date(2019, 3, 3), datetime.date(2019, 3, 4),
    # datetime.date(2019, 3, 5), datetime.date(2019, 3, 6), datetime.date(2019, 3, 7), datetime.date(2019, 3, 8),
    # .......
    # datetime.date(2019, 3, 29), datetime.date(2019, 3, 30), datetime.date(2019, 3, 31)]
    # 结束日期并不包含在这个日期范围内(事实上它是下个月的开始日期)。 这个和Python的 slice 与 range 操作行为保持一致,同样也不包含结尾。
    
    
    # 使用生成器优化这段代码
    def date_range(start,stop,step):
        if start<stop:
            yield start
            li.append(start)
            start+=step
    date_range(start_date,end_date,timedelta(days=1))
    
    # print(li)

    补充:datetime

    now = datetime.now()
    print(now)
    # 返回一个time结构
    print(
        now.timetuple())  # time.struct_time(tm_year=2019, tm_mon=3, tm_mday=5, tm_hour=9, tm_min=41, tm_sec=52, tm_wday=1, tm_yday=64, tm_isdst=-1)
    # 返回一个date类型
    print(now.date())  # 2019-03-05
    # 返回一个time类型
    print(now.time())  # 09:41:52.715695
    # 当前星期几。星期一是0,星期于是6   注意这里是方法,不是属性哦。
    print(now.weekday())  # 1
    # 当前星期几。星期一是1,星期日是7注意这里是方法,不是属性哦。
    print(now.isoweekday())  # 2
    # 修改当前时间。比如修改成当月1号
    print(now.replace(day=1))  # 2019-03-01 09:41:52.715695
    past = datetime(2010, 11, 12, 13, 14, 15, 16)  # 2010-11-12 13:14:15.000016
    print(past)
    # 进行比较运算返回的是timedelta类型
    print(now - past)  # 3034 days, 20:27:37.715679
    # 转成字符串
    strdatetime = now.strftime("%Y-%m-%d %H:%M:%S")
    print(strdatetime)  # 2019-03-05 09:41:52
    # 字符串生成datetime对象
    print(datetime.strptime(strdatetime, "%Y-%m-%d %H:%M:%S"))  # 2019-03-05 09:41:52
    返回指定年的某月:calendar
    import calendar
    
    
    def get_month(year, month):
        return calendar.month(year, month)
    
    
    # 返回指定年的日历
    def get_calendar(year):
        return calendar.calendar(year)
    
    
    # 判断某一年是否为闰年,如果是,返回True,如果不是,则返回False
    def is_leap(year):
        return calendar.isleap(year)
    
    
    # 返回某个月的weekday的第一天和这个月的所有天数
    def get_month_range(year, month):
        return calendar.monthrange(year, month)
    
    
    # 返回某个月以每一周为元素的序列
    def get_month_calendar(year, month):
        return calendar.monthcalendar(year, month)
    
    
    # 返回指定年的日历
    def get_calendar(year):
        return calendar.calendar(year)
    
    
    # 判断某一年是否为闰年,如果是,返回True,如果不是,则返回False
    def is_leap(year):
        return calendar.isleap(year)
    
    
    # 返回某个月的weekday的第一天和这个月的所有天数
    def get_month_range(year, month):
        return calendar.monthrange(year, month)  # (3, 31)
    
    
    # 返回某个月以每一周为元素的序列
    def get_month_calendar(year, month):
        return calendar.monthcalendar(year, month)
    
    
    year = 2013
    month = 8
    test_month = get_month(year, month)
    print(test_month)
    print('#' * 50)
    # print(get_calendar(year))
    print('{0}这一年是否为闰年?:{1}'.format(year, is_leap(year)))
    print(get_month_range(year, month))
    print(get_month_calendar(year, month))
    """
    """
     August 2013
    Mo Tu We Th Fr Sa Su
              1  2  3  4
     5  6  7  8  9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31
    
    ##################################################
    2013这一年是否为闰年?:False
    (3, 31)
    [[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0]]
    """
  • 相关阅读:
    洛谷 P1087 【FBI树】题解
    CF584D 【Dima and Lisa】题解
    洛谷 P1042 【乒乓球】题解
    洛谷 P2010 【回文日期】题解
    洛谷 P1233 【木棍加工】题解
    STM32嵌入式开发学习笔记(五):中断
    STM32嵌入式开发学习笔记(四):使用滴答计时器实现精准计时
    STM32嵌入式开发学习笔记(三):使用按键控制小灯
    STM32嵌入式开发学习笔记(二):将功能封装为库文件
    STM32嵌入式开发学习笔记(一)
  • 原文地址:https://www.cnblogs.com/zzy-9318/p/10478503.html
Copyright © 2011-2022 走看看