2011-08-02 13 views

Respuesta

6

I no sabe exactamente lo que quiere ... Pero tal vez usted quiera algo como esto:

(Time::days_in_month(05,2010).to_f/7)

#> 4.42857142857143

+0

Hola Michael, quiero calcular la muchas semanas hay en enero, febrero, etc ... – andkjaer

+0

desde el domingo hasta ¿lunes? Porque de lo contrario sería siempre 4. 28/7 es 4. 30/7 = 4.29 etc. –

+0

No entiendo, algunas polillas tienen 5 semanas y otras 4 ¿verdad? No puedo calcular eso? – andkjaer

3

lo que necesitaba saber cuántas semanas incluyendo parcial semanas hubo en un mes. Piense en ello como filas en un calendario. ¿Cuántas filas necesitas? Debe tener en cuenta la cantidad de días y también el día en que comienza el mes. Octubre de 2011 en realidad tiene 6 semanas únicas, por ejemplo.

Esta es mi respuesta (@date es la fecha actual):

@week_count = (0.5 + (@date.at_end_of_month.day + @date.at_beginning_of_month.wday).to_f/7.0).round 
1

Usted puede utilizar los siguientes métodos:

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 

Uso: weeks_in_month (fecha_hora)

espero que ayuda:

Gracias,

Jignesh

0

Uso de la gema 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 

número get de semanas para el mes como éste

number_of_weeks_month(Date.parse("2017-11-01"),0,Date.parse("2017-11-30")) 

este retorno 4

Cuestiones relacionadas