relative dates

C

charlie bowman

today = Time.now

how can I do this? "last_week = 7.days.ago"

I know the above won't work, but how can I get the date of last week?
 
C

Cameron McBride

how can I do this? "last_week =3D 7.days.ago"

or subtract in seconds:
last_week =3D today - 7*24*60*60

Cameron
 
A

A LeDonne

or subtract in seconds:
last_week =3D today - 7*24*60*60

Cameron

This is probably going to be easier with Date than with Time...

irb(main):001:0> require 'date'
=3D> true
irb(main):002:0> now =3D Date.today; now.strftime()
=3D> "2006-01-29"
irb(main):003:0> last_week =3D now - 7; last_week.strftime()
=3D> "2006-01-22"

If you are concerned about times, you can use DateTime in place of Date abo=
ve.

-A
 
C

charlie bowman

Thanks, I had no idea that it was so simple in Ruby!


today = Time.now
puts (today - 7)


I thought I would have to write the method myself!
 
A

A LeDonne

Thanks, I had no idea that it was so simple in Ruby!


today =3D Time.now
puts (today - 7)

Careful!

If you're using Time, you're subtracting SECONDS:
irb(main):001:0> today =3D Time.now
=3D> Mon Jan 30 00:11:51 EST 2006
irb(main):002:0> puts( today - 7 )
Mon Jan 30 00:11:44 EST 2006
=3D> nil

That's why I included the " require 'date' " in my example, and
suggested that you could use DateTime if you needed times.
 
N

Nathaniel S. H. Brown

You might want to check out RUNT:

http://runt.rubyforge.org/

DateBox might also be worth a gander:

http://datebox.inimit.com

-Nb

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nathaniel S. H. Brown http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-----Original Message-----
From: A LeDonne [mailto:[email protected]]
Sent: January 29, 2006 9:15 PM
To: ruby-talk ML; (e-mail address removed)
Subject: Re: relative dates

Thanks, I had no idea that it was so simple in Ruby!


today = Time.now
puts (today - 7)

Careful!

If you're using Time, you're subtracting SECONDS:
irb(main):001:0> today = Time.now
=> Mon Jan 30 00:11:51 EST 2006
irb(main):002:0> puts( today - 7 )
Mon Jan 30 00:11:44 EST 2006
=> nil

That's why I included the " require 'date' " in my example,
and suggested that you could use DateTime if you needed times.
I thought I would have to write the method myself!
 
E

Ezra Zygmuntowicz

In rails you are already done:
Sun Jan 22 22:59:26 PST 2006

-Ezra


This is probably going to be easier with Date than with Time...

irb(main):001:0> require 'date'
=> true
irb(main):002:0> now = Date.today; now.strftime()
=> "2006-01-29"
irb(main):003:0> last_week = now - 7; last_week.strftime()
=> "2006-01-22"

If you are concerned about times, you can use DateTime in place of
Date above.

-A

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
http://yakimaherald.com
(e-mail address removed)
blog: http://brainspl.at
 
C

charlie bowman

Your're right! as soon as I implemented it I noticed the seconds change.
I just came up with the number of seconds in a day and used that. I
would be nice if it were as simple in ruby as it is in rails, but I know
speed is more important and ease of use.....usually.
 
A

Adam Sanderson

Depending on if you need the time component, it may just be eaiser to
work with the Date object.

d = Date.today
lw = d - 7

'lw' will be 7 days ago. In this case adding and subtracting seconds
will be fine, but be careful about it, days aren't always 24 hours long
(think about daylight savings time), and it can really catch up with
you sometimes.

.adam
 
A

Alex Combas

Your're right! as soon as I implemented it I noticed the seconds change.
I just came up with the number of seconds in a day and used that. I
would be nice if it were as simple in ruby as it is in rails, but I know
speed is more important and ease of use.....usually.

What?! Speed more important than ease of use in Ruby!? Surly you jest sir!!

=3D)
 
S

Seth Thomas Rasmussen

Haha... it appears like you got ignored doubly for having the exact,
ready-made solution for his problem.

Weird world...
 
S

Seth Thomas Rasmussen

Charlie,

"I would be nice if it were as simple in ruby as it is in rails"

First, Rails is Ruby. Second, Rails is a smart grouping of stand-alone
components, one of which already has the extensions to do EXACTLY what
you mentioned in your first post.

I'm curious to know why you and the rest of the group seem so focused
on other solutions, given that something so slick already exists. I may
have glanced over the replies too quickly, but it didn't seem like
their are requirements that rule out the ActionPack solution.
 

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
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top