问题
I am working in ruby, i ahave an object containing today's datetime from database. I only want time truncating the date. How can i get that?
回答1:
Try DateTime#strftime.
DateTime.now.strftime("%H:%M")
# => "12:17"
回答2:
#!/usr/bin/ruby -w
time1 = Time.now
puts "Current Time : " + time1.hour + ":" + time1.min + ":" + time1.sec
Taken from & credit to - This TutorialsPoint Post.
回答3:
If you use it really often you can use the gem time_splitter to help you set some accessors of date, time, hour and minute for your datetime attributes. Hope it helps.
回答4:
You can check the different Rails date formats from here : Rails Date Formats
Example :
time = Time.now
time.strftime("%d %B %Y")
# => 12 December 2016
来源:https://stackoverflow.com/questions/12562804/get-time-from-datetime-variable-in-ruby