zoukankan      html  css  js  c++  java
  • python 骚操作 输入日期年获取全年所有日期输入年月获取整月日期

    啥都不多说直接上干货,代码走起:

    import re
    from copy import deepcopy
    import pandas as pd
    from dateutil.relativedelta import relativedelta
    
    def generate_date(start):
        start_err = deepcopy(start)
        if re.findall('-',start):
            start=start.replace('-','')
        assert start.isdigit(),start
        starts = deepcopy(start)
        if len(start) == 4:
            start = start + '-01-01'
            end = str(int(starts) + 1) + '-01-01'
        elif len(start) == 6:
            start = start[:4] + '-' + start[4:6] + '-' + '01'
            end = (pd.to_datetime(start) + relativedelta(months=1)).strftime('%Y-%m-%d')
        elif len(start) == 8:
            return [start]
        else:
            assert False,start_err
    
        return [i.strftime('%Y%m%d') for i in pd.date_range(start=start, end=end, freq='1d') if
                i.strftime('%Y%m%d') != end.replace('-', '')]
    
    
    print(generate_date('2019'))
    print(generate_date('201912'))
    print(generate_date('20190101'))
    print(generate_date('2019-01'))
    print(generate_date('2019-01-01'))
    print(generate_date('2019-001-01'))
    

      结果:

    ['20190101', '20190102', '20190103', '20190104', '20190105', '20190106', '20190107', '20190108', '20190109', '20190110', '20190111', '20190112', '20190113', '20190114', '20190115', '20190116', '20190117', '20190118', '20190119', '20190120', '20190121', '20190122', '20190123', '20190124', '20190125', '20190126', '20190127', '20190128', '20190129', '20190130', '20190131', '20190201', '20190202', '20190203', '20190204', '20190205', '20190206', '20190207', '20190208', '20190209', '20190210', '20190211', '20190212', '20190213', '20190214', '20190215', '20190216', '20190217', '20190218', '20190219', '20190220', '20190221', '20190222', '20190223', '20190224', '20190225', '20190226', '20190227', '20190228', '20190301', '20190302', '20190303', '20190304', '20190305', '20190306', '20190307', '20190308', '20190309', '20190310', '20190311', '20190312', '20190313', '20190314', '20190315', '20190316', '20190317', '20190318', '20190319', '20190320', '20190321', '20190322', '20190323', '20190324', '20190325', '20190326', '20190327', '20190328', '20190329', '20190330', '20190331', '20190401', '20190402', '20190403', '20190404', '20190405', '20190406', '20190407', '20190408', '20190409', '20190410', '20190411', '20190412', '20190413', '20190414', '20190415', '20190416', '20190417', '20190418', '20190419', '20190420', '20190421', '20190422', '20190423', '20190424', '20190425', '20190426', '20190427', '20190428', '20190429', '20190430', '20190501', '20190502', '20190503', '20190504', '20190505', '20190506', '20190507', '20190508', '20190509', '20190510', '20190511', '20190512', '20190513', '20190514', '20190515', '20190516', '20190517', '20190518', '20190519', '20190520', '20190521', '20190522', '20190523', '20190524', '20190525', '20190526', '20190527', '20190528', '20190529', '20190530', '20190531', '20190601', '20190602', '20190603', '20190604', '20190605', '20190606', '20190607', '20190608', '20190609', '20190610', '20190611', '20190612', '20190613', '20190614', '20190615', '20190616', '20190617', '20190618', '20190619', '20190620', '20190621', '20190622', '20190623', '20190624', '20190625', '20190626', '20190627', '20190628', '20190629', '20190630', '20190701', '20190702', '20190703', '20190704', '20190705', '20190706', '20190707', '20190708', '20190709', '20190710', '20190711', '20190712', '20190713', '20190714', '20190715', '20190716', '20190717', '20190718', '20190719', '20190720', '20190721', '20190722', '20190723', '20190724', '20190725', '20190726', '20190727', '20190728', '20190729', '20190730', '20190731', '20190801', '20190802', '20190803', '20190804', '20190805', '20190806', '20190807', '20190808', '20190809', '20190810', '20190811', '20190812', '20190813', '20190814', '20190815', '20190816', '20190817', '20190818', '20190819', '20190820', '20190821', '20190822', '20190823', '20190824', '20190825', '20190826', '20190827', '20190828', '20190829', '20190830', '20190831', '20190901', '20190902', '20190903', '20190904', '20190905', '20190906', '20190907', '20190908', '20190909', '20190910', '20190911', '20190912', '20190913', '20190914', '20190915', '20190916', '20190917', '20190918', '20190919', '20190920', '20190921', '20190922', '20190923', '20190924', '20190925', '20190926', '20190927', '20190928', '20190929', '20190930', '20191001', '20191002', '20191003', '20191004', '20191005', '20191006', '20191007', '20191008', '20191009', '20191010', '20191011', '20191012', '20191013', '20191014', '20191015', '20191016', '20191017', '20191018', '20191019', '20191020', '20191021', '20191022', '20191023', '20191024', '20191025', '20191026', '20191027', '20191028', '20191029', '20191030', '20191031', '20191101', '20191102', '20191103', '20191104', '20191105', '20191106', '20191107', '20191108', '20191109', '20191110', '20191111', '20191112', '20191113', '20191114', '20191115', '20191116', '20191117', '20191118', '20191119', '20191120', '20191121', '20191122', '20191123', '20191124', '20191125', '20191126', '20191127', '20191128', '20191129', '20191130', '20191201', '20191202', '20191203', '20191204', '20191205', '20191206', '20191207', '20191208', '20191209', '20191210', '20191211', '20191212', '20191213', '20191214', '20191215', '20191216', '20191217', '20191218', '20191219', '20191220', '20191221', '20191222', '20191223', '20191224', '20191225', '20191226', '20191227', '20191228', '20191229', '20191230', '20191231']
    ['20191201', '20191202', '20191203', '20191204', '20191205', '20191206', '20191207', '20191208', '20191209', '20191210', '20191211', '20191212', '20191213', '20191214', '20191215', '20191216', '20191217', '20191218', '20191219', '20191220', '20191221', '20191222', '20191223', '20191224', '20191225', '20191226', '20191227', '20191228', '20191229', '20191230', '20191231']
    ['20190101']
    ['20190101', '20190102', '20190103', '20190104', '20190105', '20190106', '20190107', '20190108', '20190109', '20190110', '20190111', '20190112', '20190113', '20190114', '20190115', '20190116', '20190117', '20190118', '20190119', '20190120', '20190121', '20190122', '20190123', '20190124', '20190125', '20190126', '20190127', '20190128', '20190129', '20190130', '20190131']
    ['20190101']
    Traceback (most recent call last):
      File "D:/project/tadmin/apps/query/tests.py", line 33, in <module>
        print(generate_date('2019-001-01'))
      File "D:/project/tadmin/apps/query/tests.py", line 22, in generate_date
        assert False,start_err
    AssertionError: 2019-001-01
    

      可以根据自己需要进行改编

  • 相关阅读:
    Matlab将字符串改成变量名-eval
    Workbench-材料库
    UG-装配
    FreeCAD将Macro自定义到工具栏
    FreeCAD加载ui文件显示于组合浏览器
    字符生成线条字-xdd1997
    ANSYS求解器
    常用物理量及其单位以及材料信息
    Appium | UiAutomator exited unexpectedly with code 0, signal null
    【转】windows改变Sublime选中背景颜色
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/14511339.html
Copyright © 2011-2022 走看看