提取任意开始年月日到结束年月日中间的所有日期和星期-LMLPHP

 


#!/usr/bin/python
# -*- coding: utf-8 -*-

import pandas as pd
import calendar
import numpy as np


def get_day_date(start_year, start_month):
    start_month_table = calendar.month(int(start_year), int(start_month))
    start_month_table = pd.DataFrame(start_month_table.split("\n")[2:])
    start_month_table = start_month_table[start_month_table[0] != ""]
    start_month_table = start_month_table[0].str.split()
    first_index = 7 - len(start_month_table.values[0])
    month_day = np.hstack(start_month_table.values.tolist())
    end_index = month_day.size
    month_date = ([1, 2, 3, 4, 5, 6, 7] * 6)[first_index:first_index + end_index]
    return month_day.astype("int"), month_date


def get_muti_month(month_range, start_year, start_month, start_day, end_day):
    date_data = []
    for i in range(0, month_range + 1):
        if i == month_range:
            # 结尾月 截取结尾
            new_df = pd.DataFrame()
            month_day, month_date = get_day_date(start_year, str(int(start_month) + i))
            month_day, month_date = month_day[:int(end_day)], month_date[:int(end_day)]
        elif i == 0:
            # 开始月截取开始
            new_df = pd.DataFrame()
            month_day, month_date = get_day_date(start_year, str(int(start_month) + i))
            month_day, month_date = month_day[int(start_day) - 1:len(month_day)], month_date[
                                                                                  int(start_day) - 1:len(month_day)]
        else:
            new_df = pd.DataFrame()
            month_day, month_date = get_day_date(start_year, str(int(start_month) + i))
            month_day, month_date = month_day[:len(month_day)], month_date[:len(month_day)]

        new_df["year"] = [start_year] * len(month_day)
        new_df["month"] = [str(int(start_month) + i)] * len(month_day)
        new_df["day"] = np.array(month_day).astype("str")
        new_df["date"] = month_date
        date_data.append(new_df)
    date_data = pd.concat(date_data).copy()
    date_data["year_month_day"] = date_data["year"] + "-" + date_data["month"] + "-" + date_data["day"]
    return date_data


def sp_date_every_day_month(satrt_date, end_date):
    start_year, start_month, start_day = satrt_date.split("-")
    end_year, end_month, end_day = end_date.split("-")

    if start_year == end_year and start_month == end_month:
        # 不跨年不跨年  只需要使用  开始结束日期限定即可
        new_df = pd.DataFrame()
        month_day, month_date = get_day_date(start_year, start_month)
        month_day, month_date = month_day[int(start_day) - 1:int(end_day)], month_date[int(start_day) - 1:int(end_day)]

        new_df["year"] = [start_year] * len(month_day)
        new_df["month"] = [start_month] * len(month_day)
        new_df["day"] = np.array(month_day).astype("str")
        new_df["date"] = month_date
        new_df["year_month_day"] = new_df["year"] + "-" + new_df["month"] + "-" + new_df["day"]
        return new_df


    elif start_year == end_year and start_month != end_month:
        # 不跨年跨月
        month_range = int(end_month) - int(start_month)
        new_df = get_muti_month(month_range, start_year, start_month, start_day, end_day)
        return new_df


    elif start_year != end_year:
        # 至多 跨一年
        date_list = []
        if start_month == "12":
            new_df = pd.DataFrame()
            month_day, month_date = get_day_date(start_year, start_month)
            month_day, month_date = month_day[int(start_day) - 1:len(month_day)], month_date[
                                                                                  int(start_day) - 1:len(month_day)]
            new_df["year"] = [start_year] * len(month_day)
            new_df["month"] = [start_month] * len(month_day)
            new_df["day"] = month_day.astype("str")
            new_df["date"] = month_date
            new_df["year_month_day"] = new_df["year"] + "-" + new_df["month"] + "-" + new_df["day"]
            date_list.append(new_df)

        else:
            month_range = 12 - int(start_month)
            new_df = get_muti_month(month_range, start_year, start_month, start_day, str(31))
            date_list.append(new_df)

        if end_month == "1":
            new_df = pd.DataFrame()
            month_day, month_date = get_day_date(end_year, end_month)
            month_day, month_date = month_day[:int(end_day)], month_date[:int(end_day)]
            new_df["year"] = [end_year] * len(month_day)
            new_df["month"] = [end_month] * len(month_day)
            new_df["day"] = month_day.astype("str")
            new_df["date"] = month_date
            new_df["year_month_day"] = new_df["year"] + "-" + new_df["month"] + "-" + new_df["day"]
            date_list.append(new_df)

        else:
            month_range = int(end_month) - 1
            new_df = get_muti_month(month_range, end_year, "1", "1", end_day)
            date_list.append(new_df)
        return pd.concat(date_list)


if __name__ == '__main__':
    res = sp_date_every_day_month("2022-10-3", "2022-12-24")
    print()

总结

        具体能做什么那就看自己的了

        比如可以使用到 某些关于日期 排版 等场景上的数据筛选处理

        比如执行任务计划 

        比如上班 分星期 

        比如上学课程表

        


  • 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
  • 📢本文由东方佑原创,首发于 CSDN博客🙉
  • 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨

提取任意开始年月日到结束年月日中间的所有日期和星期-LMLPHP


12-20 06:43