2011-09-14 24 views

Respuesta

136

El formato se puede hacer así (asumí que quería decir HH: MM en lugar de HH: SS, pero es fácil de cambiar):

Time.now.strftime("%d/%m/%Y %H:%M") 
#=> "14/09/2011 14:09" 

actualizado para el cambio:

d = DateTime.now 
d.strftime("%d/%m/%Y %H:%M") 
#=> "11/06/2017 18:11" 
d.next_month.strftime("%d/%m/%Y %H:%M") 
#=> "11/07/2017 18:11" 

Necesita require 'date' para este btw.

+0

sí, tienes razón, quiero decir hh: mm, y muchas gracias por tu respuesta – anonymous

+0

, pero ¿cómo puedo incremen t mes? – anonymous

+0

gracias por la ans :) – anonymous

8
time = Time.now.to_s 

time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M") 

de incremento decremento meses de uso < < >> operadores

ejemplos

datetime_month_before = DateTime.parse(time) << 1 



datetime_month_before = DateTime.now << 1 
18
require 'date' 

current_time = DateTime.now 

current_time.strftime "%d/%m/%Y %H:%M" 
# => "14/09/2011 17:02" 

current_time.next_month.strftime "%d/%m/%Y %H:%M" 
# => "14/10/2011 17:02" 
+1

Encuentro esto mucho más simple y más legible que la solución aceptada. – skagedal

4

Para la fecha:

#!/usr/bin/ruby -w 

date = Time.new 
#set 'date' equal to the current date/time. 

date = date.day.to_s + "/" + date.month.to_s + "/" + date.year.to_s 
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY 

puts date 
#output the date 

Lo anterior se mostrará, f o el ejemplo, 10/01/15

Y por el tiempo

time = Time.new 
#set 'time' equal to the current time. 

time = time.hour.to_s + ":" + time.min.to_s 
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display hour and   minute 

puts time 
#output the time 

Lo anterior se mostrará, por ejemplo, 11:33

Luego de poner juntos, se suman a la fin:

puts date + " " + time 
Cuestiones relacionadas