Getting a timestamp

Hi,

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

thank you

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.

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. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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")

On 19 Nov, 03:31, Sven S. [email protected] 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()

It’s there anything comparable in Ruby?

Thanks

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)

It’s there anything comparable in Ruby?

Thanks

maybe…
http://www.ruby-forum.com/topic/78420

2009/11/19 jp [email protected]:

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)

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

2009/11/19 Robert K. [email protected]:

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