如何从rails中的日期时间中删除毫秒?

时间:2022-12-01 16:04:05

I have to compare the date in rails to get the values after that date I have send the date as "2013-03-04T06:26:25Z"but actually the record in the db contains date as follows 2013-03-04 06:26:25.817149 so when i check with the date it also returns that record but i want records after that date. how can i remove the milliseconds from the db? please help me.

我必须比较rails中的日期以获取该日期之后的值我已将日期发送为“2013-03-04T06:26:25Z”,但实际上db中的记录包含日期,如下所示2013-03-04 06: 26:25.817149因此,当我检查日期时,它也会返回该记录,但我希望在该日期之后记录。如何从数据库中删除毫秒?请帮帮我。

3 个解决方案

#1


26  

I had a similar problem

我有类似的问题

Update your time object like this before sending it to the database :

在将时间对象发送到数据库之前更新这样的时间对象:

time.change(:usec => 0)

#2


5  

Since ruby 1.9 you can use round

由于红宝石1.9你可以使用圆形

t = Time.now.round
t.usec # => 0

#3


1  

I was also having this problem, however when I was applying the change as indicated in @Intrepidd's answer, it wasn't affecting the microseconds of my time object.

我也遇到了这个问题,但是当我按照@ Intrepidd的回答中所示应用更改时,它并没有影响我的时间对象的微秒。

Please note that (at least currently) the :usec key only works with the Time#change method, and not with the DateTime#change method.

请注意(至少目前):usec键仅适用于Time#change方法,而不适用于DateTime #change方法。

The DateTime#change method ignores the keys that it doesn't accept, so you wouldn't be able to tell that your attempted change of the microseconds didn't work unless you inspected the object further (such as with DateTime#rfc3339(9)).

DateTime #change方法忽略它不接受的键,因此除非您进一步检查对象(例如使用DateTime#rfc3339(9),否则您将无法判断您尝试更改的微秒不起作用。 ))。

So before you attempt this change, make sure that you are working with a Time object, not a DateTime object.

因此,在尝试此更改之前,请确保使用的是Time对象,而不是DateTime对象。

#1


26  

I had a similar problem

我有类似的问题

Update your time object like this before sending it to the database :

在将时间对象发送到数据库之前更新这样的时间对象:

time.change(:usec => 0)

#2


5  

Since ruby 1.9 you can use round

由于红宝石1.9你可以使用圆形

t = Time.now.round
t.usec # => 0

#3


1  

I was also having this problem, however when I was applying the change as indicated in @Intrepidd's answer, it wasn't affecting the microseconds of my time object.

我也遇到了这个问题,但是当我按照@ Intrepidd的回答中所示应用更改时,它并没有影响我的时间对象的微秒。

Please note that (at least currently) the :usec key only works with the Time#change method, and not with the DateTime#change method.

请注意(至少目前):usec键仅适用于Time#change方法,而不适用于DateTime #change方法。

The DateTime#change method ignores the keys that it doesn't accept, so you wouldn't be able to tell that your attempted change of the microseconds didn't work unless you inspected the object further (such as with DateTime#rfc3339(9)).

DateTime #change方法忽略它不接受的键,因此除非您进一步检查对象(例如使用DateTime#rfc3339(9),否则您将无法判断您尝试更改的微秒不起作用。 ))。

So before you attempt this change, make sure that you are working with a Time object, not a DateTime object.

因此,在尝试此更改之前,请确保使用的是Time对象,而不是DateTime对象。