1

我有两个 DateTime 对象,一个在过去,一个代表当前日期时间。我试图找出两者之间经过了多少分钟:

past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now  = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime

seconds = (now - past)             #result is (-83/2160)

这是不正确的。秒数应为 280,即两次之间经过的秒数。

4

1 回答 1

1

Subtracting two DateTimes returns the elapsed time in days.

So you can do:

past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now  = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime
seconds = (now - past) * 1.day
# => -3320.0

Or you could do:

seconds = (now.to_i - past.to_i)
# => -3320

※ The result is negative because of the Timezone.

past.utc
# => Wed, 03 Jul 2013 00:59:39 +0000 
now.utc
# => Wed, 03 Jul 2013 00:04:19 +0000 

You can see that now is actually older than past.

于 2013-07-03T00:30:06.317 回答