Python implementation capture a day is the week of a month

  • 2020-04-02 14:37:00
  • OfStack

I've been looking for a long time but I can't find out how to get the date of the month in Python datetime.

Helpless under the help of students, students to write a module. If you know Python has this native library, please feel free to comment.

I'll make some notes.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '####'
 
import datetime
 
 
def get_week_of_month(year, month, day):
    """
    Gets that the specified day is the week of the month
    Monday is the beginning of the 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)


Related articles: