蟒蛇提供了一種方式來輕鬆獲取當月(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))
adjusted_dom =(1 + first_day.weekday())%7周開始週日 – 2014-03-03 18:13:37
工作得很好。謝謝。 – crazyDiamond 2015-12-15 16:26:10
如果你的第一個星期開始的一個月,你可以使用整數除法的第一天:
import datetime day_of_month = datetime.datetime.now().day week_number = (day_of_month - 1) // 7 + 1
也許datetime.datetime.now()。天將給予當月的今天一天 – aeter 2010-09-27 18:11:19
@aeter:確實。添加。 – 2010-09-27 18:14:05
謝謝。 事實證明,這畢竟不是關於Python,而是關於算術:) – 2010-09-28 11:00:41
我發現了一個很簡單的辦法:
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
'isocalendar()'可以在1月1日返回53.您的實現對'weekOfMonth(2010,1,8)'返回-52。 – 2010-12-06 23:08:19
我修復了代碼並檢查了那個錯誤,代碼沒有那麼大,它完美地完成了這項工作。 – 2010-12-07 00:57:10
返回-47日期:2012-12-31,2013-12-30,2013-12-31 – 2013-01-26 16:16:56
請鏈接Python: Number of the Week in a Month
這一切都取決於當每月的第一個星期一是
這個版本還有待改進,但在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
我知道這是歲,但我花了很多時間試圖找到答案。我做了我自己的方法,並認爲我應該分享。
日曆模塊有一個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
這應該這樣做。
#! /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'
喬希的答案必須微調,以適應星期天的第一天。
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))
喬什的回答似乎是最好的,但我認爲我們應該考慮到一個星期屬於一個月只有在其週四落入該月的事實。至少這是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月份的一週。
最正確的方法是使該方法返回輸入日所屬的週數和月份名稱。
- 1. 周圍圖像matplotlib蟒蛇
- 2. 蟒蛇matplotlib:繪製3D球體圓周
- 3. 截斷日期上週一或蟒蛇
- 4. 天超出範圍一個月蟒蛇
- 5. 計數 - 蟒蛇
- 6. 蟒蛇對數
- 7. 文本(蟒蛇)的多塊
- 8. 無法在蟒蛇蟒蛇
- 9. 蟒蛇
- 10. Apache Zeppelin蟒蛇UDF到SQL
- 11. 蟒蛇數猜測
- 12. 蟒蛇...用數字
- 13. 蟒蛇,從整數
- 14. 蟒蛇
- 15. 文本數據轉換爲csv蟒蛇
- 16. 通蟒蛇會議與蟒蛇的另一個版本編寫的腳本
- 17. 剖析蟒蛇腳本
- 18. WX蟒蛇,在文本
- 19. 從文本文件蟒蛇
- 20. 降級蟒蛇版本
- 21. 文本列表蟒蛇
- 22. 蟒蛇本地範圍
- 23. PHP:將當週的週數轉換爲本月的當前週數
- 24. 蟒蛇:在matplotlib
- 25. 使用蟒蛇
- 26. 蟒蛇CV2數組numpy的
- 27. 2維數組的蟒蛇
- 28. 蟒蛇functools.partial值的參數
- 29. 刪除蟒蛇的邊數
- 30. 數據幀file.txt的蟒蛇
當你到達29日,你實際上是在第五週,對吧? – erikbwork 2010-09-27 17:59:24
一個星期是在一個月的第一天開始還是一直在星期一開始?或者總是在星期天?要麼... ? – 2010-09-27 18:02:02
「本月的星期」並不完全是一個常用的概念。您需要告訴我們您的定義是什麼,例如「本月的周」......特別是每個月的哪一週,例如一週從星期一開始,假設今天是星期天和月的第一天:你想要(上個月,第5周)還是(當前月,第-1周或第0周)或其他什麼? – 2010-09-28 00:47:44