# Finding last date

**URL:** <https://www.ruby-forum.com/t/finding-last-date/91294>\
**Category:** Ruby\
**Created:** [April 6, 2007, 11:00am UTC](https://www.ruby-forum.com/t/finding-last-date/91294 "2007-04-06T11:00:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 6, 2007, 11:00am UTC](https://www.ruby-forum.com/t/finding-last-date/91294/1 "2007-04-06T11:00:49Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 6, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/finding-last-date/91294/2 "2007-04-06T12:11:07Z")

</div>

On 06.04.2007 11:00, Dipesh B. 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

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 6, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/finding-last-date/91294/3 "2007-04-06T14:46:19Z")

</div>

On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh B. 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/rdoc/more/classes/Numeric.html

```

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 6, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/finding-last-date/91294/4 "2007-04-06T19:17:17Z")

</div>

Cool, thanks, thats great info.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 6, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/finding-last-date/91294/5 "2007-04-06T19:26:20Z")

</div>

On Apr 6, 6:05 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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”

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 7, 2007, 4:36am UTC](https://www.ruby-forum.com/t/finding-last-date/91294/6 "2007-04-07T04:36:48Z")

</div>

On Apr 6, 12:21 pm, removed\_email\_address@domain.invalid 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. 🙂

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2007, 4:11am UTC](https://www.ruby-forum.com/t/finding-last-date/91294/7 "2007-04-08T04:11:48Z")

</div>

On Fri, 06 Apr 2007 10:21:01 -0700, srinsriram wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/finding-last-date/91294/8 "2007-04-08T14:46:19Z")

</div>

Hi,

Am Freitag, 06. Apr 2007, 21:45:43 +0900 schrieb Jeremy H.:

> On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh B. 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/rdoc/more/classes/Numeric.html
> 
> ```

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

Bertram
