Python实现获取某天是某个月中的第几周
Jun 10, 2016 pm 03:17 PM
python
which week
找了半天竟然没找到,如何在Python的datetime处理上,获取某年某月某日,是属于这个月的第几周。
无奈之下求助同学,同学给写了一个模块。【如果你知道Python有这个原生的库,请不吝赐教】
我稍作整理记录在下。
复制代码 代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '####'
import datetime
def get_week_of_month(year, month, day):
"""
获取指定的某天是某个月中的第几周
周一作为一周的开始
"""
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)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '####'
import datetime
def get_week_of_month(year, month, day):
"""
获取指定的某天是某个月中的第几周
周一作为一周的开始
"""
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)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis
