जवाबों:
स्वरूपण इस तरह से किया जा सकता है (मैंने मान लिया था कि आपको HH: MM के बजाय HH: SS है, लेकिन इसे बदलना आसान है:)
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
स्थानांतरण के लिए अद्यतन:
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'इस btw के लिए की जरूरत है ।
Dateवस्तु प्राप्त करने का सबसे अच्छा तरीका था । वैसे भी, मैंने आपकी टिप्पणी को वर्तमान रूबी संस्करणों के लिए अंततः इस उत्तर को अपडेट करने के अवसर के रूप में लिया है।
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"
दिनांक के लिए:
#!/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
ऊपर प्रदर्शित होगा, उदाहरण के लिए, 10/01/15
और समय के लिए
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
उपरोक्त प्रदर्शित करेगा, उदाहरण के लिए, 11:33
फिर इसे एक साथ रखने के लिए, अंत में जोड़ें:
puts date + " " + time