gpt4 book ai didi

Ruby:比较两个时间对象的日期

转载 作者:数据小太阳 更新时间:2023-10-29 07:02:38 25 4
gpt4 key购买 nike

在 Ruby 中比较两个 Time 对象的日期的最佳方法是什么?

我有两个对象,例如:

time_1 = Time.new(2012,12,10,10,10)
time_2 = Time.new(2012,12,11,10,10)

在此示例中,日期比较应返回 false。

否则,相同日期但不同时间应返回 true:

time_1 = Time.new(2012,12,10,10,10)
time_2 = Time.new(2012,12,10,11,10)

我尝试使用适用于 DateTime 对象的 .to_date,但 Time 不支持它。

最佳答案

只需要 stdlib 的“日期”部分,然后比较日期:

require "date"
time1.to_date == time2.to_date

工作完成。

关于Ruby:比较两个时间对象的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com