Ruby:将日期和时间对象组合成一个日期时间 [英] Ruby: combine Date and Time objects into a DateTime

查看:52
本文介绍了Ruby:将日期和时间对象组合成一个日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但我找不到好的或明确的答案.将 Ruby 日期和时间对象(对象,而不是字符串)组合成单个 DateTime 对象的最佳和最有效的方法是什么?

Simple question, but I can't find a good or definitive answer. What is the best and most efficient way to combine Ruby Date and Time objects (objects, not strings) into a single DateTime object?

推荐答案

我发现了这个,但它没有你希望的那么优雅:

I found this, but it's not as elegant you would hope:

d = Date.new(2012, 8, 29)
t = Time.now
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)

顺便说一下,ruby Time 对象还存储了年、月和日,因此您在创建 DateTime 时会将其丢弃.

By the way, the ruby Time object also stores a year, month, and day, so you would be throwing that away when you create the DateTime.

这篇关于Ruby:将日期和时间对象组合成一个日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆