How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?
DateTime.strptime
can handle seconds since epoch. The number must be converted to a string:
require 'date'
DateTime.strptime("1318996912",'%s')
How to convert timestamp with ruby
From the string you can parse it to DateTime passing the format, and then format the output string:
date_time = '10/18/2107 14:08:09'
puts DateTime.strptime(date_time, '%m/%d/%Y %H:%M:%S')
.strftime('%Y M%m %d, %a %H:%M:%S %Z')
Note it's Tuesday and the time zone it's specified as 00:00.
Ruby - Convert formatted date to timestamp
Your date string is in RFC3339 format. You can parse it into a DateTime
object, then convert it to Time
and finally to a UNIX timestamp.
require 'date'
DateTime.rfc3339('2015-05-27T07:39:59Z')
#=> #<DateTime: 2015-05-27T07:39:59+00:00 ((2457170j,27599s,0n),+0s,2299161j)>
DateTime.rfc3339('2015-05-27T07:39:59Z').to_time
#=> 2015-05-27 09:39:59 +0200
DateTime.rfc3339('2015-05-27T07:39:59Z').to_time.to_i
#=> 1432712399
For a more general approach, you can use DateTime.parse
instead of DateTime.rfc3339
, but it is better to use the more specific method if you know the format, because it prevents errors due to ambiguities in the date string. If you have a custom format, you can use DateTime.strptime
to parse it
Convert from timestamp to time
Ruby's Time.at
expects seconds (with fraction). Per the definition
Creates a new Time object with the value given by time, the given number of
seconds_with_frac
, orseconds
andmicroseconds_with_frac
since the Epoch.
The value you provided is milliseconds. Change it to
Time.at(1500999892331/1000)
=> 2017-07-25 21:54:52 +0530
t.to_date
=> Tue, 25 Jul 2017
Convert timestamp string without timezone to Time object in known timezone
I ended up making use of a gem called "Time of Day", it will take a string and give you a time of day object, you can then convert that to local time in your current Time.zone
on any date you chose:
Time.zone = "Europe/London"
time_string = "23/06/2021 13:46"
parts = time_string.split(" ")
date = Date.parse(parts[0])
tod = Tod::TimeOfDay.parse(parts[1])
tod.on date # => Wed, 23 Jun 2021 13:46:00 BST +01:00
How to convert BSON::Timestamp to ruby time and vice versa
You can convert a BSON::Timestamp
to a BSON::ByteBuffer
using the #to_bson
method.
You can then convert the BSON::ByteBuffer
to an integer (#get_int64
) that represents the number of milliseconds since the epoch.
Then use Time::at
to convert that integer to a Time
object
date_time = DateTime.new(2021,8,30)
date_time.to_time
#=> 2021-08-30 00:00:00 +0000
date_time.to_time.to_i
#=> 1630281600
timestamp = BSON::Timestamp.from_bson(date_time.to_bson)
#=> #<BSON::Timestamp:0x00007fffe31da4a8 @seconds=379, @increment=2488994816>
timestamp.to_bson.get_int64 / 1000
#=> 1630281600
Time.at(timestamp.to_bson.get_int64 / 1000).utc
#=> 2021-08-30 00:00:00 UTC
how to convert this (month/day/year ) date format to actual timestamp in rails
I think you just need format that string to valid format to convert to date, something like this:
old_format = '6/21/2021'.split('/')
old_format[0], old_format[1] = old_format[1], old_format[0]
old_format.join('/').to_date
How to convert timestamp?
Perhaps it is called ISO 8601. You can accept this form and turn it into a time object by doing this:
require "time"
Time.iso8601("2013-01-24T23:42:59Z")
# => 2013-01-24 23:42:59 UTC
Related Topics
How to Get a Reference to a 'Dynamic' Object Call
Ruby Instance Method & Conditional Local Variable Assignment with Same Name
How to Escape All Characters with Special Meaning in Regex
Understanding Ruby Method Parameters Syntax
Why Does the Script Affect Everything on My Rails 3 App Even When Cased in This Code
Attr_Accessor, Not Able to Access Property
Sending Form Data to Remote Rails Application Using Httparty
How to Set Text Area Properties Inside Controller Page Using Ror
PDF Generation Hangs Using PDFkit and Wkhtmotopdf
Emulating Int64 Overflows in Ruby
Rails App Has Trouble with Inter-Model Saving
Ancestry Gem in Rails and Mutli Nesting
Ror: How to Get My Microposts to Show Up
What Ruby and Rails Developers Ought to Know
Differencebetween 'Raise "Foo"' and 'Raise Exception.New("Foo")'
Listen Error: Unable to Monitor Directories for Changes