# Getting a timestamp

**URL:** <https://www.ruby-forum.com/t/getting-a-timestamp/179179>\
**Category:** Ruby\
**Created:** [November 19, 2009, 4:10am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179 "2009-11-19T04:10:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 4:10am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/1 "2009-11-19T04:10:43Z")

</div>

Hi,

I would like to get a timestamp from a Date object. What is the best  
aproach for this?

thank you

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 4:25am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/2 "2009-11-19T04:25:21Z")

</div>

jp wrote:

> Hi,
> 
> I would like to get a timestamp from a Date object. What is the best  
> aproach for this?
> 
> thank you

I think you want a DateTime or Time object, I believe a Date object only  
represents a date, not a date and  
time unless perhaps ‘date at midnight’ counts.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 4:26am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/3 "2009-11-19T04:26:31Z")

</div>

On Nov 18, 2009, at 10:10 PM, jp wrote:

> Hi,
> 
> I would like to get a timestamp from a Date object. What is the best  
> aproach for this?
> 
> thank you

I’m not sure if this is the most effective, but

d = Date.new(2009, 11, 18)  
ts = d.strftime(‘%s’).to\_i

seems to work for me, giving the timestamp for 2009-11-10 00:00:00 GMT

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 4:31am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/4 "2009-11-19T04:31:48Z")

</div>

Don’t know if this is what you’re looking for but whenever I need a  
serial-style timestamp (like for a filename). I just do this:

Time.now.strftime("%Y%m%d%H%M%S")

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 10:20am UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/5 "2009-11-19T10:20:36Z")

</div>

On 19 Nov, 03:31, Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > represents a date, not a date and time unless perhaps ‘date at midnight’  
> > counts.

Hi,

It’s enough to have the milliseconds since a given date in the past,  
for example java has this:  
[http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime()](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime())

It’s there anything comparable in Ruby?

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/6 "2009-11-19T14:10:03Z")

</div>

jp wrote:

> > 
> 
> Hi,
> 
> It’s enough to have the milliseconds since a given date in the past,  
> for example java has this:  
> [Date (Java 2 Platform SE 5.0)](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime())
> 
> It’s there anything comparable in Ruby?
> 
> Thanks

maybe…  
[http://www.ruby-forum.com/topic/78420](http://www.ruby-forum.com/topic/78420)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/7 "2009-11-19T17:33:11Z")

</div>

2009/11/19 jp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > I think you want a DateTime or Time object, I believe a Date object only  
> > > > represents a date, not a date and time unless perhaps ‘date at midnight’  
> > > > counts.
> 
> Hi,
> 
> It’s enough to have the milliseconds since a given date in the past,  
> for example java has this:  
> [Date (Java 2 Platform SE 5.0)](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime())
> 
> It’s there anything comparable in Ruby?

Hint: there is documentation…

irb(main):001:0\> Time.now.to\_i  
=\> 1258647502  
irb(main):002:0\> Time.at(0)  
=\> 1970-01-01 01:00:00 +0100  
irb(main):003:0\> Time.at(0).to\_i  
=\> 0

Cheers

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 19, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/getting-a-timestamp/179179/8 "2009-11-19T17:34:56Z")

</div>

2009/11/19 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > 
> > 
> > It’s there anything comparable in Ruby?
> 
> Hint: there is documentation…
> 
> irb(main):001:0\> Time.now.to\_i  
> =\> 1258647502  
> irb(main):002:0\> Time.at(0)  
> =\> 1970-01-01 01:00:00 +0100  
> irb(main):003:0\> Time.at(0).to\_i  
> =\> 0

PS: I forgot

irb(main):007:0\> t=Time.now  
=\> 2009-11-19 17:20:25 +0100  
irb(main):008:0\> t.to\_i  
=\> 1258647625  
irb(main):009:0\> t.to\_f  
=\> 1258647625.44141  
irb(main):010:0\> t.usec  
=\> 441411

Cheers

robert
