Obtain the week of a certain day in a month using Python.

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.