2011-09-14 10 views

Odpowiedz

136

Formatowanie można zrobić tak (Przypuszczałem chodziło hh: mm zamiast HH: SS, ale to można łatwo zmienić):

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

Aktualizacja dla przesunięcia:

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" 

Musisz require 'date' dla tego btw.

+0

tak masz rację, mam na myśli hh: mm, i dziękuję bardzo za odpowiedź – anonymous

+0

, ale jak mogę t miesiąc? – anonymous

+0

dziękuję za ans :) – anonymous

8
time = Time.now.to_s 

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

na przyrost ubytek miesięcy użytkowania < < >> operatorów

przykłady

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

Uważam, że jest to o wiele prostsze i czytelniejsze niż przyjęte rozwiązanie. – skagedal

4

Na bieżąco:

#!/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 

Powyższy pokaże, f lub przykład, 10/01/15

A do czasu

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 

Powyższy wyświetli na przykład 11:33

Następnie umieścić je razem, dodać do koniec:

puts date + " " + time 
Powiązane problemy