Home > database >  Ruby DateTime and epoch conversion
Ruby DateTime and epoch conversion

Time:01-08

I'm using pluck to get the datetime.

ModelName.where("created_at >= ? ", Time.zone.now).pluck(:created_at)

it is giving me output in this format [[17 Jan 2022 18:03:20 IST 05:30], [20 Jan 2022 13:06:25 IST 05:30]]

Is there way to convert this DateTime in epochtime, or use pluck in such a way to get epochtime instead of getting DateTime like this?

Is there a way to convert epochtime into this "17 Jan 2022 18:03:20 IST 05:30" DateTime format?

I need to compare params[:created_at] which is in epochtime from the data which I'm getting from DB.

CodePudding user response:

Convert to Time with .to_time and then to Unix Time with to_i. For example:

created_at = ModelName.where("created_at >= ? ", Time.zone.now).pluck(:created_at)
created_at.to_time.to_i
  •  Tags:  
  • Related