Answers:
可以像这样进行格式化(我假设您的意思是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'
为此顺便说一句。
Date
对象的最佳方法。无论如何,我以您的评论为契机,最终为当前Ruby版本更新了此答案。
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