2012-07-03 9 views

Respuesta

15

¿Qué le parece usar el operador String.format (%)? De esta manera:

x = '%02d' % t.hour 
puts x    # prints 07 if t.hour equals 7 
22

Podría ser vale la pena analizar Time#strftime si tienes intención de poner sus tiempos juntos en una cadena legible o algo por el estilo.

Por ejemplo,

t = Time.now 
t.strftime('%H') 
    #=> returns a 0-padded string of the hour, like "07" 
t.strftime('%M') 
    #=> returns a 0-padded string of the minute, like "03" 
t.strftime('%H:%M') 
    #=> "07:03" 
7

Puede probar esto!

Time.now.to_formatted_s(:time) 
Cuestiones relacionadas