我正在开发一个使用 Ruby 2.2 和 Rails 4 以及 sqlite 数据库的项目,其中时间差计算错误。
我为此目的使用了 TimeDifference 方法。
在我的数据库中有一个表 MachineAttendance。
InTime 存储为 2017 年 2 月 12 日星期二 20:30:00 UTC +00:00
OutTime 存储为 2017 年 2 月 12 日星期二 05:30:00 UTC +00:00
@c1.in_time.strftime("%I:%M %p")
"08:30 PM"
@c1.out_time.strftime("%I:%M %p")
"05:30 AM"
And my method is
time_diff=TimeDifference.between(@c1.in_time.strftime('%H:%M:%S'), @c1.out_time.strftime('%H:%M:%S')).in_hours.to_f
its giving 15.0 hrs which is wrong
as it should be 9.0 hrs.
有人可以告诉我如何获得正确的差异......谢谢:)
最佳答案
您可以尝试以下对我来说的工作
Time.at((@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a693b24657f7e557e63676f274a693b246364" rel="noreferrer noopener nofollow">[email protected]</a>_time).round.abs).utc.strftime "%H:%M"
它将返回类似“09:00”的时间
关于ruby-on-rails-4 - Rails 4 中的时差计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42242375/