在Epoch和Back中将Ruby时间戳转换为秒

时间:2021-04-03 11:27:38

I have a field in my database that is stored as a timestamp, I need to output this in seconds in EPOCH time.

我的数据库中有一个存储为时间戳的字段,我需要在EPOCH时间内以秒为单位输出。

3 个解决方案

#1


38  

Supposing your timestamp is a Ruby Time object:

假设您的时间戳是Ruby Time对象:

puts time_stamp.strftime('%s')
puts time_stamp.to_i
timestamp = Time.at(628232400)

In case it is a DateTime object, you have the strftime and strptime methods at your disposal.

如果它是DateTime对象,您可以使用strftime和strptime方法。

#2


23  

Returns number of seconds since epoch:

返回自纪元以来的秒数:

time = Time.now.to_i  

Returns second since epoch which includes microseconds:

从纪元开始返回第二个,包括微秒:

time = Time.now.to_f

#3


8  

Time.now.to_i 

returns the Epoch. Same goes for:

返回*。同样适用于:

time = Time.now
time.to_i

#1


38  

Supposing your timestamp is a Ruby Time object:

假设您的时间戳是Ruby Time对象:

puts time_stamp.strftime('%s')
puts time_stamp.to_i
timestamp = Time.at(628232400)

In case it is a DateTime object, you have the strftime and strptime methods at your disposal.

如果它是DateTime对象,您可以使用strftime和strptime方法。

#2


23  

Returns number of seconds since epoch:

返回自纪元以来的秒数:

time = Time.now.to_i  

Returns second since epoch which includes microseconds:

从纪元开始返回第二个,包括微秒:

time = Time.now.to_f

#3


8  

Time.now.to_i 

returns the Epoch. Same goes for:

返回*。同样适用于:

time = Time.now
time.to_i