165

How to get UTC timestamp in Ruby?

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

9 Answers9

278

You could use: Time.now.to_i.

manzhikov
  • 3,778
  • 3
  • 20
  • 22
125
time = Time.now.getutc

Rationale: In my eyes a timestamp is exactly that: A point in time. This can be accurately represented with an object. If you need anything else, a scalar value, e.g. seconds since the Unix epoch, 100-ns intervals since 1601 or maybe a string for display purposes or storing the timestamp in a database, you can readily get that from the object. But that depends very much on your intended use.

Saying that »a true timestamp is the number of seconds since the Unix epoch« is a little missing the point, as that is one way of representing a point in time, but it also needs additional information to even know that you're dealing with a time and not a number. A Time object solves this problem nicely by representing a point in time and also being explicit about what it is.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 104
    That gives the time object, you should to Time.now.to_i for a true timestamp – rafamvc Jan 11 '12 at 23:58
  • 3
    In my opninion an object is timestampy enough and you have still all the freedom you need. Reducing it to a scalar value is in most cases not really necessary. – Joey Jan 12 '12 at 06:27
  • 2
    Timestamps are relative to the epoch -- doesn't matter which timezone you're in. manzhikov's answer is the better one. – Ryan Mohr Aug 31 '12 at 22:19
  • 1
    This post answers the OP question. – kd8azz Jan 21 '13 at 02:34
  • 3
    While a nice explanation of why to not use a unix timestamp, this doesn't answer the OP question (nor the same question I had when I found this post). Unix timestamps are required in many cases (e.g., for APIs). Even after the explanation, this answer didn't give the actual way to get the timestamp. – stuckj Nov 09 '13 at 02:45
  • 5
    @stuckj, at no point the question mentions a Unix timestamp. I just continue to get downvotes for this answer because people seem to think that »timestamp == Unix time«, hence the explanation. – Joey Nov 09 '13 at 08:24
  • I can only assume that you are getting downvotes because most developers probably found this question through a search and read it as how to get a unix timestamp and hence consider this wrong. I think you would get less downvotes (and still keep your point) if you just added a little addendum to your answer mentioning using to_i to get the unix timestamp if a unix timestamp was desired. I'd rescind my downvote with that. :-) – stuckj Nov 10 '13 at 18:44
  • 1
    @Joey: a timestamp is a time *stamp*, not a *time*. It's used for saving to a database, including in a filename, printing on a report or web page, etc. It is not an in-memory object. That's not a time *stamp*. Picture a rubber stamp that adjust to show the date & time, and that you dip in ink and then apply to a piece of paper. An electronic time *stamp* is the metaphorical equivalent. – iconoclast Jun 17 '14 at 17:57
  • @iconoclast: The intended use still dictates what you need, then. Saving to a database needs a Date object (at least if the API to access said database is any good), putting in a file name needs an ISO 8601 string, as do date/times on reports or web pages. Most people here seemingly want seconds since Unix epoch. Which is unusable for the scenarios you outlined as well. – Joey Jun 17 '14 at 21:58
  • There's also `Time.now.utc` which is 3 characters shorter and slightly easier to read. – Dennis Jun 26 '14 at 14:42
  • 2
    If the OP meant "timestamp" to mean the number of seconds elapsed since an epoch, then the question itself is useless because the word "UTC" is redundant. Either this is the correct answer or the question is unanswerable without refinement. – Don Cruickshank Nov 16 '14 at 17:12
  • @rafamvc You should say "a true ***Unix*** timestamp. `.to_i` gives the Epoch Time. But sometimes the more human-readable timestamp is desirable too. – GDP2 Jul 27 '20 at 04:39
55

The default formatting is not very useful, in my opinion. I prefer ISO8601 as it's sortable, relatively compact and widely recognized:

>> require 'time'
=> true
>> Time.now.utc.iso8601
=> "2011-07-28T23:14:04Z"
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
  • 1
    Note that this method comes from ActiveSupport, not in the standard library http://www.rubydoc.info/docs/rails/ActiveSupport%2FTimeWithZone:iso8601 – n0nick Oct 03 '17 at 07:13
  • 4
    @n0nick are you sure, even in 2.0? I know I've used it on "vanilla" installs without rails etc., and it's in `/usr/share/ruby/2.0/time.rb`. – Tim Sylvester Oct 04 '17 at 16:48
  • 1
    `iso8601` is in the Ruby standard library: you need to `require 'time'` – collimarco Nov 08 '21 at 13:45
27

if you need a human-readable timestamp (like rails migration has) ex. "20190527141340"

Time.now.utc.to_formatted_s(:number)  # using Rails

Time.now.utc.strftime("%Y%m%d%H%M%S") # using Ruby
Kate Kasinskaya
  • 823
  • 10
  • 10
18

Usually timestamp has no timezone.

% irb
> Time.now.to_i == Time.now.getutc.to_i
=> true
Yuki Matsukura
  • 448
  • 3
  • 11
  • 1
    `to_f` will be better – xis Apr 08 '14 at 22:57
  • 1
    depends on the situation. – Yuki Matsukura Apr 09 '14 at 09:40
  • 1
    When *does* a timestamp have a timezone? – Dennis Jun 26 '14 at 14:44
  • 1
    timestamp does not related to time zone. In other words, timestamp is UTC. – Yuki Matsukura Jun 27 '14 at 03:51
  • 1
    @YukiMatsukura Actually, that's not entirely accurate either. A time stamp doesn't even represent any time zone, so "timestamp is UTC" is true as much as "timestamp is elephant poop". The problem is in representation. Most devs represent it as Unix time stamp (which does not indicate UTC or any other time zone as it's just an integer value). – Pelle Jun 02 '17 at 17:59
7

What good is a timestamp with its granularity given in seconds? I find it much more practical working with Time.now.to_f. Heck, you may even throw a to_s.sub('.','') to get rid of the decimal point, or perform a typecast like this: Integer(1e6*Time.now.to_f).

joshweir
  • 5,427
  • 3
  • 39
  • 59
chava
  • 379
  • 4
  • 7
  • thanks. I used this: `Integer(1e3*Time.now.to_f)` so that my server side ruby timestamp was the same as client side javascript `Date.now()` – joshweir Aug 01 '19 at 21:46
2

Time.utc(2010, 05, 17)

August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
-3

time = Time.zone.now()

It will work as

irb> Time.zone.now
=> 2017-12-02 12:06:41 UTC
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Asterisk
  • 129
  • 1
  • 11
-6

The proper way is to do a Time.now.getutc.to_i to get the proper timestamp amount as simply displaying the integer need not always be same as the utc timestamp due to time zone differences.

Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31