python是否提供了一种轻松获取当月(1:4)的方法?
为了使用直接分割,需要根据当月第一天的位置(在一周内)调整您所查看日期的月份日期。因此,如果您的月份恰好在星期一(一周的第一天)开始,您可以按照上面的建议进行划分。但是,如果月份从星期三开始,您将要添加2然后进行除法。这全部封装在下面的函数中。
from math import ceil
def week_of_month(dt):
""" Returns the week of the month for the specified date.
"""
first_day = dt.replace(day=1)
dom = dt.day
adjusted_dom = dom + first_day.weekday()
return int(ceil(adjusted_dom/7.0))
这应该做到这一点。
#! /usr/bin/env python2
import calendar, datetime
#FUNCTIONS
def week_of_month(date):
"""Determines the week (number) of the month"""
#Calendar object. 6 = Start on Sunday, 0 = Start on Monday
cal_object = calendar.Calendar(6)
month_calendar_dates = cal_object.itermonthdates(date.year,date.month)
day_of_week = 1
week_number = 1
for day in month_calendar_dates:
#add a week and reset day of week
if day_of_week > 7:
week_number += 1
day_of_week = 1
if date == day:
break
else:
day_of_week += 1
return week_number
#MAIN
example_date = datetime.date(2015,9,21)
print "Week",str(week_of_month(example_date))
#Returns 'Week 4'
Josh的回答似乎是最好的,但我认为我们应该考虑到这样一个事实,即一个星期只有在它的星期四落入那个月才属于一个月。至少那是what the iso says。
根据该标准,一个月可以有长达5周的时间。一天可能属于一个月,但它所属的那一周可能不是。
我考虑到只是添加一个简单的
if (first_day.weekday()>3) :
return ret_val-1
else:
return ret_val
其中ret_val恰好是Josh的计算值。在2017年6月(有5周)和2017年9月进行测试。通过'2017-09-01'返回0,因为该日属于不属于9月的一周。
最正确的方法是让方法返回输入日所属的周数和月份名。
移动到每月的最后一天,从数学导入ceil分为7
def week_of_month(dt):
""" Returns the week of the month for the specified date.
"""
# weekday from monday == 0 ---> sunday == 6
last_day_of_week_of_month = dt.day + (7 - (1 + dt.weekday()))
return int(ceil(last_day_of_week_of_month/7.0))
如果您的第一周从该月的第一天开始,您可以使用整数除法:
import datetime day_of_month = datetime.datetime.now().day week_number = (day_of_month - 1) // 7 + 1
我知道这已经有好几年了,但我花了很多时间试图找到这个答案。我做了自己的方法,并认为我应该分享。
日历模块具有monthcalendar方法,该方法返回2D数组,其中每行代表一周。例如:
import calendar
calendar.monthcalendar(2015,9)
结果:
[[0,0,1,2,3,4,5],
[6,7,8,9,10,11,12],
[13,14,15,16,17,18,19],
[20,21,22,23,24,25,26],
[27,28,29,30,0,0,0]]
numpy在这里你的朋友在哪里。而且我在美国所以我希望这个星期在周日开始,第一周标记为1:
import calendar
import numpy as np
calendar.setfirstweekday(6)
def get_week_of_month(year, month, day):
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x==day)[0][0] + 1
return(week_of_month)
get_week_of_month(2015,9,14)
回报
3
看看python calendar模块
这个版本可以改进,但作为python模块(日期时间和日历)的第一眼,我做了这个解决方案,我希望可能有用:
from datetime import datetime
n = datetime.now()
#from django.utils.timezone import now
#n = now() #if you use django with timezone
from calendar import Calendar
cal = Calendar() # week starts Monday
#cal = Calendar(6) # week stars Sunday
weeks = cal.monthdayscalendar(n.year, n.month)
for x in range(len(weeks)):
if now.day in weeks[x]:
print x+1
Josh的答案必须稍微调整,以适应周日的第一天。
def get_week_of_month(date):
first_day = date.replace(day=1)
day_of_month = date.day
if(first_day.weekday() == 6):
adjusted_dom = (1 + first_day.weekday()) / 7
else:
adjusted_dom = day_of_month + first_day.weekday()
return int(ceil(adjusted_dom/7.0))
查看包装Pendulum
>>> dt = pendulum.parse('2018-09-30')
>>> dt.week_of_month
5
我找到了一个非常简单的方法:
import datetime
def week(year, month, day):
first_week_month = datetime.datetime(year, month, 1).isocalendar()[1]
if month == 1 and first_week_month > 10:
first_week_month = 0
user_date = datetime.datetime(year, month, day).isocalendar()[1]
if month == 1 and user_date > 10:
user_date = 0
return user_date - first_week_month
如果第一周返回0
请链接到Python: Number of the Week in a Month
这一切都取决于当月的第一个星期一