Get time from DateTime Variable in Ruby

只谈情不闲聊 提交于 2021-02-06 09:46:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!