Yanıtlar:
Biçimlendirme şu şekilde yapılabilir (HH: SS yerine HH: MM'yi kastettiğinizi varsaydım, ancak değiştirilmesi kolaydır):
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
Vites değiştirme için güncellendi:
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"
require 'date'Bu btw için ihtiyacınız var .
Datenesneyi elde etmenin en iyi yolu olduğuna dair bir öneri değil, değişimle ilgiliydi . Her neyse, yorumunuzu nihayet mevcut Ruby sürümleri için bu cevabı güncelleme fırsatı olarak değerlendirdim.
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"
Tarih için:
#!/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
Yukarıda, örneğin 10/01/15 görüntülenecektir.
Ve zaman için
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
Yukarıdakiler, örneğin, 11:33
Sonra bir araya getirmek için sonuna ekleyin:
puts date + " " + time