date time subtraction

A

aidy

Hi,

I am trying to do a subtraction of the date and time in the format
below with the current date and time

"2009-07-01 18:24:20"

Will I need to tokenise this string and do individual number
comparisons?

Thanks

Aidy
 
J

Joel VanderWerf

aidy said:
Hi,

I am trying to do a subtraction of the date and time in the format
below with the current date and time

"2009-07-01 18:24:20"

Will I need to tokenise this string and do individual number
comparisons?

irb(main):007:0> require 'time'
=> true
irb(main):009:0> t = Time.parse "2009-07-01 18:24:20"
=> Wed Jul 01 18:24:20 -0700 2009
irb(main):010:0> t.to_i
=> 1246497860
irb(main):011:0> t.day
=> 1
irb(main):012:0> t.hour
=> 18

See the Time class for details. You can also directly compare time
objects (without using to_i to get seconds since the epoch):

irb(main):013:0> Time.now < Time.now
=> true
 
J

Joel VanderWerf

Joel said:
See the Time class for details. You can also directly compare time
objects (without using to_i to get seconds since the epoch):

And you can also subtract Time objects to get seconds:

irb(main):016:0> t - Time.now
=> 27032.573318
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top