How to get a time object for two weeks ago?

lo there all,

i am wrestling with this for a bit now. how do i get a time object for
two
weeks ago ?
if
t = Time.now, how do i get t - 2 weeks ?

thanks

shawn bright wrote:

lo there all,

i am wrestling with this for a bit now. how do i get a time object for
two
weeks ago ?
if
t = Time.now, how do i get t - 2 weeks ?

thanks

Time is stored in seconds so:

Time.now - 606024*14 = 2 weeks ago

Dan

Hi,

You could check out chronic[1] as well - maybe it is an overkill for
this single case, but if you have more queries like this, take a look
for sure. It handles even much more complicated things like

last friday at 20:00
afternoon yesterday

etc.

Cheers,
Peter

[1] http://chronic.rubyforge.org/
__
http://www.rubyrailways.com

shawn bright wrote:

lo there all,

i am wrestling with this for a bit now. how do i get a time object for
two
weeks ago ?
if
t = Time.now, how do i get t - 2 weeks ?

thanks

If you use rails or include active support:

require ‘rubygems’
require ‘active_support’
2.weeks.ago

Dan

Dan F. wrote:

Time is stored in seconds so:

Time.now - 606024*14 = 2 weeks ago

Dan

class DateTime
def to_time
Time.local( *strftime( “%Y,%m,%d,%H,%M,%S” ).split( “,” ).
map{|str| str.to_i } )
end
end

puts (DateTime.now - 14).to_time

thanks much, working all better now.
shawn

William J. wrote:

thanks
Time.local( *strftime( “%Y,%m,%d,%H,%M,%S” ).split( “,” ).
map{|str| str.to_i } )
end
end

puts (DateTime.now - 14).to_time

class DateTime
def to_time
Time.local( *strftime( “%Y %m %d %H %M %S” ).split )
end
end

puts (DateTime.now - 14).to_time

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs