python获取指定日期之间的月份(亲测有用)

2020-02-08 13:17:38 阅读 0评论 2

温馨提示:

最后更新时间2020-02-08 13:17:38,若内容或图片失效,请留言反馈!

python获取指定日期之间的月份(亲测有用)

 

import datetime
 
    from dateutil import rrule
    def get_each_month(start_month, end_month):
        if str(start_month).count('.') != 1 or str(end_month).count('.') != 1:
            print("Parameter Error: Pls input a string such as '2019.01'")
            return []
        if int(str(start_month).split('.')[1]) > 12 or int(str(end_month).split('.')[1]) > 12:
            print('Parameter Error: Pls input correct month range such as between 1 to 12')
            return []
        if int(str(start_month).split('.')[1]) == 0 or int(str(end_month).split('.')[1]) == 12:
            print('Parameter Error: Pls input correct month range such as between 1 to 12')
            return []
        start = datetime.datetime.strptime(start_month, "%Y.%m")
        end = datetime.datetime.strptime(end_month, "%Y.%m")
        month_count = rrule.rrule(rrule.MONTHLY, dtstart=start, until=end).count()  # 计算总月份数
        if end < start:
            print("Parameter Error: Pls input right date range,start_month can't latter than end_month")
            return []
        else:
            list_month = []
            year = int(str(start)[:7].split('-')[0])  # 截取起始年份
            for m in range(month_count):  # 利用range函数填充结果列表
                month = int(str(start)[:7].split('-')[1])  # 截取起始月份,写在for循环里,作为每次迭代的累加基数
                month = month + m
                if month > 12:
                    if month % 12 > 0:
                        month = month % 12  # 计算结果大于12,取余数
                        if month == 1:
                            year += 1  # 只需在1月份的时候对年份加1,注意year的初始化在for循环外
                    else:
                        month = 12
                if len(str(month)) == 1:
                    list_month.append(str(year) + '.0' + str(month))
                else:
                    list_month.append(str(year) + '.' + str(month))
            return list_month
 
 
    month_list = get_each_month(str("2019.4"),str(datetime.datetime.now().strftime('%Y.%m')))
    print(month_list)
 
 
 
 

 

 

文章版权声明:除非注明,否则均为林羽凡原创文章,转载或复制请以超链接形式并注明出处。
林羽凡

林羽凡V

林羽凡博客,是一个记录自己生活、互联网技术的原创独立博客 Linyufan.Com

4259 文章
16545 评论
20844977 浏览

了解一下?

  • 了解一下?

优质服务器推荐

  • 软云_RuanCloud

活跃朋友

最新留言

    欢迎加入:heo-出家人:
    邹江 • 时间 15小时前
    WC,凡哥也整上软云了:heo-出家人:
    邹江 • 时间 15小时前
    我能坚持每天5公里,就不错了!
    八字123 • 时间 21小时前
    你是在保密局工作吗,家里嘴这么严,才让你知道。????
    jeffer.z的博客 • 时间 1天前
    欢迎加入!!!
    老张博客 • 时间 1天前
    @ 林羽凡还好,还好。问题不大,下雨什么的也没什么感觉
    obaby • 时间 2024年03月26日 20:27:24
    摔提挺严重呀。祝早日康复!:heo-感动:
    粽叶加米 • 时间 2024年03月26日 11:47:30
    这一看就好痛,本好奇宝宝曾经让汽修厂的升降机烟断过脚趾头。单腿蹦哒了三个月。:heo-泪奔:
    obaby • 时间 2024年03月26日 10:47:39
    @ 林羽凡开玩笑的,不会跑,我被cc,还是他给我解决的,当然备份还是必须的。数据安全第一,用谁的服务器都得备份。
    jeffer.z的博客 • 时间 2024年03月26日 09:12:10
    我也是软云????,一夜之间都在用,这要是jun跑路了,他就在博客圈火了,明天我也写一篇我和他的事情,很有意思。
    jeffer.z的博客 • 时间 2024年03月25日 23:30:05
    这得好好休息一段时间了
    dujun • 时间 2024年03月25日 21:29:29
    也是蛮厉害的人,完骑车才骑十多公里。
    惭愧呀,我得慢慢努力啦。
    争取早日摆脱菜腿称号。:heo-笑哭:
    Net • 时间 2024年03月25日 17:17:13

再了解一下?

  • 林羽凡待办
取消
支付宝二维码
支付宝二维码
微信二维码