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
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
aidy wrote:
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
Joel VanderWerf wrote:
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
On 1 July, 18:50, Joel VanderWerf [email protected] wrote:
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
Thanks Joel.
Aidy
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs