开发者

Ruby: clean way to piece together date and time

开发者 https://www.devze.com 2023-02-13 08:31 出处:网络
After collecting info from the following form: What\'s the ea开发者_JAVA技巧siest way to piece together the two parts into a single date object in Ruby? (I wrote some crappy way to do this, and is

After collecting info from the following form:

Ruby: clean way to piece together date and time

What's the ea开发者_JAVA技巧siest way to piece together the two parts into a single date object in Ruby? (I wrote some crappy way to do this, and is curious if there is a cleaner method)


require 'date'
date, time = %w(2011-02-26 17:00)
dt = DateTime.parse("#{date} #{time}:00")
dt.to_s # => 2011-02-26T17:00:00+00:00

Note the assumption of UTC; you'll have to append your own time zone if needed.


>> Time.local(*("2011-02-26".split("-") + "17:00".split(":")))
=> Sat Feb 26 17:00:00 0100 2011
>> Time.parse(["2011-02-26", "17:00"].join(" "))
=> Sat Feb 26 17:00:00 0100 2011


If you have 2010-02-26 as params[:date] and 17:00 as params[:time], then you can do

Time.parse("#{params[:date] #{params[:time]}}")
0

精彩评论

暂无评论...
验证码 换一张
取 消