# Behaviour of Date

**URL:** <https://rubytalk.org/t/behaviour-of-date/73553>\
**Category:** ruby-talk\
**Created:** [5 April 2016 14:21 UTC](https://rubytalk.org/t/behaviour-of-date/73553 "2016-04-05T14:21:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![SAURAV\_KOTHARI](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/saurav_kothari/32/1919_2.png) [@SAURAV\_KOTHARI](https://rubytalk.org/u/SAURAV_KOTHARI)\
**Post date:** [5 April 2016 14:21 UTC](https://rubytalk.org/t/behaviour-of-date/73553/1 "2016-04-05T14:21:38Z")

</div>

Hello,

I am facing this situation where the how the date calculations are made  
seem to not make sense.

Say the date is set to \*31st March 2016\*

&nbsp;&nbsp;d = Date.parse('31-03-2016')  
&nbsp;&nbsp;// d = Thu, 31 Mar 2016

&nbsp;&nbsp;d + 1.month = Sat, 30 Apr 2016

&nbsp;&nbsp;d - 1.month = Mon, 29 Feb 2016

Is this how it is supposed to be evaluating? Shouldn't adding \*1.month\* to  
date and subtracting it return the original date?

Regards,  
Saurav Kothari

---

<div class="post-metadata">

**Author:** ![Bryce\_Kerley](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@Bryce\_Kerley](https://rubytalk.org/u/Bryce_Kerley)\
**Post date:** [5 April 2016 14:23 UTC](https://rubytalk.org/t/behaviour-of-date/73553/2 "2016-04-05T14:23:26Z")

</div>

That’s what happens, but not what you’re doing. `Date#+` doesn’t mutate the date, it returns a new one.

> **···**
>
> On 5 Apr 2016, at 10:21, Saurav Kothari wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;d = Date.parse('31-03-2016')  
> > &nbsp;&nbsp;&nbsp;&nbsp;// d = Thu, 31 Mar 2016
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;d + 1.month = Sat, 30 Apr 2016
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;d - 1.month = Mon, 29 Feb 2016
> > 
> > Is this how it is supposed to be evaluating? Shouldn't adding \*1.month\* to  
> > date and subtracting it return the original date?

---

<div class="post-metadata">

**Author:** ![SAURAV\_KOTHARI](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/saurav_kothari/32/1919_2.png) [@SAURAV\_KOTHARI](https://rubytalk.org/u/SAURAV_KOTHARI)\
**Post date:** [5 April 2016 14:27 UTC](https://rubytalk.org/t/behaviour-of-date/73553/3 "2016-04-05T14:27:30Z")

</div>

Yes. Modifying the date is not my intention in this example.

But say it were. Say I were to to modify the current date to some date in  
the future. A simple reversal of steps will not give me the original value  
for certain dates.

Can something else be done to make such a calculation reversible?

Saurav Kothari

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 April 2016 19:57 UTC](https://rubytalk.org/t/behaviour-of-date/73553/4 "2016-04-05T19:57:34Z")

</div>

Considering the way #month behaves (which obviously is intentional),  
that is indeed not possible for some dates.

For reference here a (working) code example:

&nbsp;&nbsp;require "date"  
&nbsp;&nbsp;require "active\_support"  
&nbsp;&nbsp;require "active\_support/core\_ext"

&nbsp;&nbsp;Date.parse("2016-03-31") + 1.month - 1.month # =\> Wed, 30 Mar 2016

&nbsp;&nbsp;Date.parse("2016-03-31") - 1.month # =\> Mon, 29 Feb 2016  
&nbsp;&nbsp;Date.parse("2016-03-30") - 1.month # =\> Mon, 29 Feb 2016  
&nbsp;&nbsp;Date.parse("2016-03-29") - 1.month # =\> Mon, 29 Feb 2016  
&nbsp;&nbsp;Date.parse("2016-03-28") - 1.month # =\> Sun, 28 Feb 2016

&nbsp;&nbsp;Date.parse("2016-03-31") + 1.month # =\> Sat, 30 Apr 2016  
&nbsp;&nbsp;Date.parse("2016-03-30") + 1.month # =\> Sat, 30 Apr 2016

&nbsp;&nbsp;Date.parse(2016-03-31) + 12.month # =\> Fri, 31 Mar 2017 (ok)

&nbsp;&nbsp;d = Date.parse("2016-03-31")  
&nbsp;&nbsp;12.times { d = d + 1.month }  
&nbsp;&nbsp;d # =\> Tue, 28 Mar 2017 (!!!)

Regards,  
Marcus

> **···**
>
> Am 05.04.2016 um 16:21 schrieb Saurav Kothari:
> 
> > Is this how it is supposed to be evaluating? Shouldn't adding /1.month/  
> > to date and subtracting it return the original date?
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Andrew\_Vit](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Andrew\_Vit](https://rubytalk.org/u/Andrew_Vit)\
**Post date:** [7 April 2016 03:34 UTC](https://rubytalk.org/t/behaviour-of-date/73553/5 "2016-04-07T03:34:15Z")

</div>

Ruby also has `\<\<` and `\>\>` methods on Date that do the same thing as  
ActiveSupport's `date + 1.month`:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mar31 = Date.new(2016, 3, 31)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;feb29 = mar31 \<\< 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mar29 = feb29 \>\> 1

A date object only holds a particular day, there is no special notion  
of anchoring it to the end of the month:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;feb29 = Date.new(2016, 2, -1)

You can create a date like that, but it doesn't have memory of the  
negative offset that was passed in, it just becomes the 29th day of the  
month.

If you want to, you could create your own wrapper for Date which holds  
this original value, something like this I just wrote for fun:

> <https://gist.github.com/avit/96f07db8a2ed83c436aff0ed61466e3a>

Andrew Vit

> **···**
>
> On 2016-04-05 07:21, Saurav Kothari wrote:
> 
> > Is this how it is supposed to be evaluating? Shouldn't adding /1.month/  
> > to date and subtracting it return the original date?

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [5 April 2016 15:11 UTC](https://rubytalk.org/t/behaviour-of-date/73553/6 "2016-04-05T15:11:07Z")

</div>

what certain dates?

irb(main):006:0\> d = Date.parse('31-03-2016')  
=\> #\<Date: 2016-03-31 ((2457479j,0s,0n),+0s,2299161j)\>

irb(main):008:0\> d = d + 30  
=\> #\<Date: 2016-04-30 ((2457509j,0s,0n),+0s,2299161j)\>

irb(main):009:0\> d - 30  
=\> #\<Date: 2016-03-31 ((2457479j,0s,0n),+0s,2299161j)\>

> **···**
>
> On Tue, 2016-04-05 at 19:57 +0530, Saurav Kothari wrote:
> 
> > Yes. Modifying the date is not my intention in this example.
> > 
> > But say it were. Say I were to to modify the current date to some date in the  
> > future. A simple reversal of steps will not give me the original value for  
> > certain dates.
> > 
> > Can something else be done to make such a calculation reversible?
> > 
> > Saurav Kothari

---

<div class="post-metadata">

**Author:** ![SAURAV\_KOTHARI](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/saurav_kothari/32/1919_2.png) [@SAURAV\_KOTHARI](https://rubytalk.org/u/SAURAV_KOTHARI)\
**Post date:** [5 April 2016 15:34 UTC](https://rubytalk.org/t/behaviour-of-date/73553/7 "2016-04-05T15:34:43Z")

</div>

Reid, the examples you have demonstrated are calculations where you add and  
\*substract\* \*days \*which will certainly be reversible.  
But in the event of adding/subtractin \*month \*(as in the first thread), you  
can see that the operation is not reversible. Can that be made reversible?

We already use a system where we set a future date (+ 1.month) for further  
actions and do not store the original date. We use -1.month to get the  
original date whenever we need it but that fails for a few cases as shown.

We could migrate to adding/subtracting 30.days in the future but how do we  
handle existing date?

Saurav Kothari

---

<div class="post-metadata">

**Author:** ![SAURAV\_KOTHARI](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/saurav_kothari/32/1919_2.png) [@SAURAV\_KOTHARI](https://rubytalk.org/u/SAURAV_KOTHARI)\
**Post date:** [6 April 2016 05:16 UTC](https://rubytalk.org/t/behaviour-of-date/73553/8 "2016-04-06T05:16:37Z")

</div>

Thank you @Martin for putting it like I was intending to. I put up a wrong  
example in the original post.

@Marcus: yes this is indeed the quirk that has me puzzled. So that is just  
how it works internally. Will have to explore other options (like +/-  
30.days).

Thank you all.

Saurav Kothari

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [5 April 2016 18:47 UTC](https://rubytalk.org/t/behaviour-of-date/73553/9 "2016-04-05T18:47:29Z")

</div>

I believe he is referring to the fact that Date("2016-03-31") + 1.month -  
1.month = Date("2016-03-30"). I don't see what can be done about that,  
since two days in March (30, 31), map to a single day in April (30), so the  
mapping is clearly not reversible.

martin

> **···**
>
> On Tue, Apr 5, 2016 at 10:32 AM, \<sto.mar@web.de\> wrote:
> 
> > Am 05.04.2016 um 17:34 schrieb Saurav Kothari:  
> > \> Reid, the examples you have demonstrated are calculations where you add  
> > \> and \*substract\* /days /which will certainly be reversible.  
> > \> But in the event of adding/subtractin /month /(as in the first thread),  
> > \> you can see that the operation is not reversible. Can that be made  
> > \> reversible?
> > 
> > Saurav, you did \_not\_ show in your first post that the operation is  
> > not reversible, you added / subtracted 1.month from the same date  
> > (2016-03-31), because the original date saved in d was not changed,  
> > as Bryce already pointed out.
> > 
> > Note also that Integer#month is not part of Ruby core or stdlib.  
> > So without knowing what gems you required it's not easy to check  
> > your example:
> > 
> > $ irb  
> > 2.3.0 :001 \> require "date"  
> > =\> true  
> > 2.3.0 :002 \> d = Date.parse('31-03-2016')  
> > =\> #\<Date: 2016-03-31 ((2457479j,0s,0n),+0s,2299161j)\>  
> > 2.3.0 :003 \> d = d + 1.month # NoMethodError !!!
> > 
> > (I assume that's what you meant to do.)
> > 
> > You are possibly using Rails?
> > 
> > Regards,  
> > Marcus
> > 
> > --  
> > GitHub: [https://github.com/stomar/](https://github.com/stomar/)  
> > PGP: 0x6B3A101A
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 April 2016 17:32 UTC](https://rubytalk.org/t/behaviour-of-date/73553/10 "2016-04-05T17:32:51Z")

</div>

Saurav, you did \_not\_ show in your first post that the operation is  
not reversible, you added / subtracted 1.month from the same date  
(2016-03-31), because the original date saved in d was not changed,  
as Bryce already pointed out.

Note also that Integer#month is not part of Ruby core or stdlib.  
So without knowing what gems you required it's not easy to check  
your example:

$ irb  
2.3.0 :001 \> require "date"  
=\> true  
2.3.0 :002 \> d = Date.parse('31-03-2016')  
=\> #\<Date: 2016-03-31 ((2457479j,0s,0n),+0s,2299161j)\>  
2.3.0 :003 \> d = d + 1.month # NoMethodError !!!

(I assume that's what you meant to do.)

You are possibly using Rails?

Regards,  
Marcus

> **···**
>
> Am 05.04.2016 um 17:34 schrieb Saurav Kothari:
> 
> > Reid, the examples you have demonstrated are calculations where you add  
> > and \*substract\* /days /which will certainly be reversible.  
> > But in the event of adding/subtractin /month /(as in the first thread),  
> > you can see that the operation is not reversible. Can that be made  
> > reversible?
> 
> --  
> GitHub: [https://github.com/stomar/](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [6 April 2016 10:07 UTC](https://rubytalk.org/t/behaviour-of-date/73553/11 "2016-04-06T10:07:19Z")

</div>

The problem of working with durations is that what a month is is  
ill-defined in that sense. So the designer of the API needs to define  
something that makes sense in edge cases.

For example, the intention of adding a month is generally speaking to  
increment the month number in the date, or reset to 1 if 12 incrementing  
the year.

But that leaves you with situations like adding a month to 31 March,  
because 31 April does not exist. You need to define what to do there, and  
the choice is to keep the "increment the month by one", rather than jumping  
to May. For that to be possible, the natural choice is "same day or end of  
the month".

And that implies that for some dates, adding a month gives the same date.  
Therefore you cannot reverse that unless you store the original value.

You need to get the business rule right taking this ambiguity into account.  
For example, if the app switches to 30.days then it has to be OK that 1  
July yields 31 July, and that 31 January may yield 2 March.

The Active Support Core Extensions guide documents edge cases of these APIs.

> **···**
>
> --  
> Sent from Gmail Mobile

---

<div class="post-metadata">

**Author:** ![Murad\_Yusufov](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Murad\_Yusufov](https://rubytalk.org/u/Murad_Yusufov)\
**Post date:** [6 April 2016 07:55 UTC](https://rubytalk.org/t/behaviour-of-date/73553/12 "2016-04-06T07:55:37Z")

</div>

You can use "end\_of\_month" method for this particular case.  
[http://apidock.com/rails/DateAndTime/Calculations/end\_of\_month](http://apidock.com/rails/DateAndTime/Calculations/end_of_month)

> **···**
>
> On Wed, 6 Apr 2016 at 08:17, Saurav Kothari \<sauravkothari2@gmail.com\> wrote:
> 
> > Thank you @Martin for putting it like I was intending to. I put up a wrong  
> > example in the original post.
> > 
> > @Marcus: yes this is indeed the quirk that has me puzzled. So that is just  
> > how it works internally. Will have to explore other options (like +/-  
> > 30.days).
> > 
> > Thank you all.
> > 
> > Saurav Kothari
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
