Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding last date

Reply
Thread Tools

Finding last date

 
 
Dipesh Batheja
Guest
Posts: n/a
 
      04-06-2007
Hi,
Is there any instance method in Ruby for Date type, to find previous
date. For example if I have date - "04/05/07" in an object "date", then
something like "date.previous" returns me date - "04/04/07".

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-06-2007
On 06.04.2007 11:00, Dipesh Batheja wrote:
> Hi,
> Is there any instance method in Ruby for Date type, to find previous
> date. For example if I have date - "04/05/07" in an object "date", then
> something like "date.previous" returns me date - "04/04/07".
>

$ irb -r date
irb(main):002:0> Date.today.to_s
=> "2007-04-06"
irb(main):003:0> (Date.today - 1).to_s
=> "2007-04-05"
irb(main):004:0>

robert
 
Reply With Quote
 
 
 
 
Jeremy Hinegardner
Guest
Posts: n/a
 
      04-06-2007
On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh Batheja wrote:
> Hi,
> Is there any instance method in Ruby for Date type, to find previous
> date. For example if I have date - "04/05/07" in an object "date", then
> something like "date.previous" returns me date - "04/04/07".


The Date object can inherently do date arithmetic:

irb(main):001:0> require 'date'
irb(main):002:0> today = Date.today
irb(main):003:0> yesterday = today - 1
irb(main):004:0> puts "Today : #{today}"
Today : 2007-04-06
irb(main):006:0> puts "Yesterday : #{yesterday}"
Yesterday : 2007-04-05

So if you would like a today.prev then you can easily add #prev.
Date#succ already exists.

class Date
def prev
self - 1
end
end

If you want even more interesting time/date utility functions add in the
facets gem and try out the 'more/times' items, you can do things like
1.week.ago and 3.hours.from_now.

http://facets.rubyforge.org/src/doc/...s/Numeric.html

enjoy,

-jeremy

--
================================================== ======================
Jeremy Hinegardner


 
Reply With Quote
 
Dipesh Batheja
Guest
Posts: n/a
 
      04-06-2007
Cool, thanks, thats great info.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
srinsriram@gmail.com
Guest
Posts: n/a
 
      04-06-2007
On Apr 6, 6:05 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 06.04.2007 11:00, Dipesh Batheja wrote:> Hi,
> > Is there any instance method in Ruby for Date type, to find previous
> > date. For example if I have date - "04/05/07" in an object "date", then
> > something like "date.previous" returns me date - "04/04/07".

>
> $ irb -r date
> irb(main):002:0> Date.today.to_s
> => "2007-04-06"
> irb(main):003:0> (Date.today - 1).to_s
> => "2007-04-05"
> irb(main):004:0>
>
> robert


I just noticed that whitespace near the -1 makes a difference in the
evaluation in irb. is this expected?

irb(main):006:0> (Date.today -1).to_s
=> "2007-04-06"

irb(main):008:0> (Date.today - 1).to_s
=> "2007-04-05"

 
Reply With Quote
 
Sam Smoot
Guest
Posts: n/a
 
      04-07-2007
On Apr 6, 12:21 pm, srinsri...@gmail.com wrote:
> I just noticed that whitespace near the -1 makes a difference in the
> evaluation in irb. is this expected?
>
> irb(main):006:0> (Date.today -1).to_s
> => "2007-04-06"
>
> irb(main):008:0> (Date.today - 1).to_s
> => "2007-04-05"


Yes. "-1" is "negative one", "- 1" is "minus one". So the former is
interpreted as passing the value -1 to the method
Date::today(sg=ITALY), while the latter is interpreted as calling
Date#- on the instance returned from Date::today and passing 1 as the
parameter to Date#-.

So it just goes to show, spacing matters, so better to be clear than
invalid.

If you think about it, if the left-hand-side contains a ".", so a
method call, the language parser can't interpret what you mean in this
example without giving significance to the whitespace.

If you try the same without a trailing method on the LHS, for example
"1 -1", then just like in math, the meaning becomes unambiguos, since
without interpreting the minus symbol as a call to Fixnum#-, there is
no expression, and the line would otherwise just be a syntax error.

So the parser is forgiving when it can be I suppose. Me, I prefer to
just use whitespace always unless I mean to actually represent a
negative number. To do otherwise just feels sloppy to me.

 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      04-08-2007
On Fri, 06 Apr 2007 10:21:01 -0700, srinsriram wrote:

> On Apr 6, 6:05 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>> On 06.04.2007 11:00, Dipesh Batheja wrote:> Hi,
>> > Is there any instance method in Ruby for Date type, to find previous
>> > date. For example if I have date - "04/05/07" in an object "date",
>> > then something like "date.previous" returns me date - "04/04/07".

>>
>> $ irb -r date
>> irb(main):002:0> Date.today.to_s
>> => "2007-04-06"
>> irb(main):003:0> (Date.today - 1).to_s => "2007-04-05"
>> irb(main):004:0>
>>
>> robert

>
> I just noticed that whitespace near the -1 makes a difference in the
> evaluation in irb. is this expected?
>
> irb(main):006:0> (Date.today -1).to_s => "2007-04-06"
>
> irb(main):008:0> (Date.today - 1).to_s => "2007-04-05"


Yeah. The first way passes the value -1 as a parameter to Date.today.
The second way does math.

Ruby has lots of ambiguous operators like that (<< versus <<HEREDOC for
example), your best strategy is to always put whitespace around your
binary operators.

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      04-08-2007
Hi,

Am Freitag, 06. Apr 2007, 21:45:43 +0900 schrieb Jeremy Hinegardner:
> On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh Batheja wrote:
> If you want even more interesting time/date utility functions add in the
> facets gem and try out the 'more/times' items, you can do things like
> 1.week.ago and 3.hours.from_now.
>
> http://facets.rubyforge.org/src/doc/...s/Numeric.html


Very nice. But unfortunately a month is not alway 30 days
and a year not always 365.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date last Accessed vs Date Modified Roedy Green Java 1 02-22-2008 09:41 AM
Finding is a given date is a month end date?? rsujatha@gmail.com Java 10 05-15-2006 06:02 AM
Finding Date of last update? Josh Computer Support 4 11-27-2005 11:31 AM
Finding date 1 day earlier than a given date! Edward Perl Misc 3 09-08-2004 04:51 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57