Q
Rails周月數
8
A
回答
6
我不知道你想要什麼......但也許你想是這樣的:
(Time::days_in_month(05,2010).to_f/7)
#> 4.42857142857143
3
我需要知道多少個星期,包括部分一個月裏有一個星期。把它想成日曆中的行。你需要多少行?你必須考慮天數以及這個月的開始日期。例如2011年10月實際上有6個獨特的周。
這就是我的回答(@date是當前日期):
@week_count = (0.5 + (@date.at_end_of_month.day + @date.at_beginning_of_month.wday).to_f/7.0).round
1
您可以使用以下方法:
WEEK_NUMBER_FORMAT = '%W'
# Returns the first day of month.
# If invoked without any arguments, this would return the
# first day of current month
def first_day_of_month(date_time=Time.now)
date_time.beginning_of_month
end
# Returns the last day of month.
# If invoked without any arguments, this would return the
# last day of current month
def last_day_of_month(date_time=Time.now)
date_time.end_of_month
end
# Returns the week number in the year in which the specified date_time lies.
# If invoked without any arguments, this would return the
# the week number in the current year
def week_number(date_time=Time.now)
date_time.strftime(WEEK_NUMBER_FORMAT).to_i
end
# Returns the number of weeks in the month in which the specified date_time lies.
# If invoked without any arguments, this would return the
# the number of weeks in the current month
def weeks_in_month(date_time=Time.now)
week_number(last_day_of_month(date_time)) - week_number(first_day_of_month(date_time)) + 1
end
用法:weeks_in_month(DATE_TIME)
希望它幫助:
謝謝,
周爲一個月Jignesh
0
使用寶石week_of_month
d = Date.new(2012,1,1)
d.total_weeks
=> 5
0
def number_of_weeks_month(start_of_month, count, end_of_month)
if start_date > end_of_month
return count
else
number_of_weeks_month(start_date.end_of_week + 1, count + 1, end_of_month)
end
end
GET號這樣
number_of_weeks_month(Date.parse("2017-11-01"),0,Date.parse("2017-11-30"))
這回4
相關問題
- 1. 週數到月份
- 2. 數的週末每月
- 3. 蟒蛇 - 本月的週數
- 4. 本月的週日數
- 5. 周月2014年6月
- 6. Rails 4 +根據HighCharts中的日,周,月和年過濾數據
- 7. 如何(月/周/年)
- 8. 周和月排序
- 9. PHP:將當週的週數轉換爲本月的當前週數
- 10. 爲了讓週一至週六當月
- 11. MySQL在本月內選擇週數
- 12. 獲取Joda-Time的月份週數
- 13. 計算給定月份的週數
- 14. LINQ的總和由月或數週
- 15. 每週總時間和月總時數
- 16. 如何獲得一個月的週數
- 17. 計算一個月內的週數
- 18. 在一個月週日的總數
- 19. 查找一個月中的週數iphone
- 20. 轉換整數值到年,月,周,天
- 21. SQL獲得每月和每週數據
- 22. 將一週數轉換爲一個月
- 23. 將每日數據彙總爲每週自定義4周月
- 24. 從週數開始計算日期 - 年份從3月開始 - 第1周= 3月第1周 - PHP
- 25. Rails - select_date - 限制月數?
- 26. SQL中每月的一週
- 27. 每日/每週/每月榜
- 28. DB2 - 獲取月份的周
- 29. DateOffset:每月的一週
- 30. sas每週總計,每月
邁克爾你好,我想算算許多星期有一月,二月等... – andkjaer
從星期日到星期日週一?因爲否則它總是4. 28/7是4. 30/7 = 4.29等 –
不明白,有些飛蛾有5周,其他4對嗎?我可以計算嗎? – andkjaer