This article mainly introduces how many weeks of a month can be obtained through Python. the code in this article shows how many days of a month can be obtained, if you need it, you can refer to it for a long time and cannot find it. in The datetime processing of Python, it is the week of the month to obtain the date of a certain month.
However, I asked for help and wrote a module for me. [If you know that Python has this native library, please do not hesitate to inform me]
I will record it later.
The code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
_ Author _ = '####'
Import datetime
Def get_week_of_month (year, month, day ):
"""
Obtain the week number of a specified day in a month.
Monday as the beginning of a week
"""
End = int (datetime. datetime (year, month, day). strftime ("% W "))
Begin = int (datetime. datetime (year, month, 1). strftime ("% W "))
Return end-begin + 1
If _ name _ = '_ main __':
Print get_week_of_month (2015, 1, 4)
Print get_week_of_month (2015, 1, 5)
Print get_week_of_month (2015, 1, 15)
Print get_week_of_month (2015, 1, 18)