The best way to compare two dates without a respect to their timezone difference?
我有两个日期:一个在'UTC',另一个在'America / Los_Angeles'。 我如何比较这两个忽略他们的时区差异,即我想检查他们的年,月,日,小时......是否彼此相等?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | local_date_string = '2014-09-19T09:00:00' local_date = local_date_string.to_datetime time_zone_name = 'America/Los_Angeles' timezone = ActiveSupport::TimeZone[time_zone_name] date_with_zone = ActiveSupport::TimeWithZone.new(nil, timezone, local_date) date_utc = date_with_zone.utc local_date_with_zone_from_utc = date_utc.in_time_zone(timezone) puts local_date.hour => 9 puts local_date_with_zone_from_utc.hour => 9 how to compare_ignoring_tz_diff(local_date, local_date_with_zone_from_utc) ? |
听起来你正在寻找
如果你有两次带区域,那么在两者上调用它并比较结果即
1 2 3 | def compare_without_tz(first, second) first.time == second.time end |
这假设两个参数都是