Is there any way to quickly calculate the penultimate working day of the month using Python?

Click on " Python Crawler and Data Mining " above to pay attention

Reply to " Books " to get a total of 10 e-books on Python from entry to advanced

now

Day

chicken

Soup

The younger brother left to join the army and the aunt died, and the evening went to the court to look at it.

Hello everyone, I am Pippi.

I. Introduction

A few days ago, I asked a Pythonbasic question in the Python group [Yuliang], and I will share it with you here.

Is there any way to quickly calculate the penultimate working day of the month? I have searched CSDN and asked ChatGPT but there is no good solution?

2. Implementation process

Later [铃Maksim] asked ChatGPT and got an answer. The preliminary test seemed to have no problem. The code is as follows:

import datetime


def get_third_last_workday(year, month):
    # 求出当月最后一天
    last_day = datetime.date(year, month, 1) + datetime.timedelta(days=31)
    last_day -= datetime.timedelta(days=last_day.day)
    while True:
        # 判断最后一天是周几
        if last_day.weekday() < 5:
            break
        last_day -= datetime.timedelta(days=1)
    # 向前推算两个工作日得到倒数第三个工作日
    third_last_workday = last_day - datetime.timedelta(days=2)
    while True:
        # 判断是否为工作日
        if third_last_workday.weekday() < 5:
            break
        third_last_workday -= datetime.timedelta(days=1)
    return third_last_workday


# 示例
print(get_third_last_workday(2023, 6))  # 输出:2023-06-28

Solved the problem of fans smoothly. There are many ways, all roads lead to Rome, as long as it can solve the problem.

3. Summary

Hello everyone, I am Pippi. This article mainly takes stock of a Pythonbasic problem. Aiming at this problem, the article gives specific analysis and code implementation to help fans solve the problem smoothly.

Finally, I would like to thank the fan [Yu Liang] for asking questions, thank [铃Maksim] for the ideas and code analysis, and thank [Mo Angry] and others for participating in the learning and exchange.

[Supplementary questions] Warm reminder, when you ask questions in the group. You can pay attention to the following points: if it involves large file data, you can desensitize the data, send some demo data (meaning small files), and then paste some code (the kind that can be copied), and remember to send the screenshot of the error report (complete cut ). If there are not many codes, just send the code text directly. If the code exceeds 50 lines, just send a .py file.

ed44d759752afbc32316d3819e03f6e8.png

If you have any problems during the learning process, please feel free to contact me to solve them (my WeChat: pdcfighting1). At the request of fans, I have created some high-quality Python paid learning exchange groups and paid order receiving groups. Welcome everyone to join me Python learning exchange group and order receiving group!

565767c234b53eec13afbc0d040bb363.png

Friends, hurry up and practice it! If you encounter any problems during the learning process, please add me as a friend, and I will pull you into the Python learning exchange group to discuss learning together.

cd19bf981a8c2fae1882c3964f40e4cd.jpeg

------------------- End -------------------

Recommendations for past wonderful articles:

334d0fdb22edabb841a6e92c37d2ede0.png

Welcome everyone to like, leave a message, forward, repost, thank you for your company and support

If you want to join the Python learning group, please reply in the background [ join the group ]

Thousands of rivers and thousands of mountains are always in love, can you click [ Looking ]

/Today's Message Topic/

Just say a few words~~

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/131566337