Trouble advancing date attribute

Frederick C. wrote:

I think i’d rather do
new_due = old_due >> 1

If you are going to roll your own version you need to worry about
things like adding a month to the 31st of august (your code will try
and set the date to the 31st of september, which will fail.
:slight_smile:

Fred

That’s awesome! I knew there would be a clean way for this. This is much
better than validating the initial due date value to be between the 1st
and 28th of the month :slight_smile:

Yep, we found that around the beginning of the thread and Fredrick
pointed out the << and >> methods of Date which does exactly what he
wanted. I didn’t know about previous_month or next_month. They are
definitely more clear and self documenting, although << and >> are part
of ruby base so they will work w/ calculations outside of rails. Looks
to me like they do the exact same thing.

-Bill

[email protected] wrote:

x.strftime("%D")
y.strftime("%D")
=> #<Date: 4908893/2,0,2299161>

a = Time.parse("#{time}")

as 9104-07-08 in my view.
7100 years).

Fred


Sincerely,

William P.

Hmm, is next_month and previous_month something you added or something
added by a plugin you are using. I grepped my rails directory and I get
nothing back. I tried it in console and get a no method error. I tried
it in a view, same undefined method, however, << and >> work everytime.
I also search the docs and the api docs, and nothing.

Date.today >> 1 = 2007-12-15

-Bill

William P. wrote:

Although this has been cleared up, there is the further issue related

time = Time.now.utc

=> “12/12/07”
=> “12/12/07”

=> Fri Mar 02 00:00:00 EST 2007
On Nov 12, 10:40 am, Frederick C. [email protected]


Sincerely,

William P.

On 15 Nov 2007, at 16:23, William P. wrote:

Hmm, is next_month and previous_month something you added or
something added by a plugin you are using. I grepped my rails
directory and I get nothing back. I tried it in console and get a no
method error. I tried it in a view, same undefined method, however,
<< and >> work everytime. I also search the docs and the api docs,
and nothing.

They are part of active support, and are defined in Time only, not Date.

Fred

Cool, thanks. I didn’t try Time since we were working with date objects.

-Bill

Frederick C. wrote:

Yep, we found that around the beginning of the thread and Fredrick

Although this has been cleared up, there is the further issue

y.strftime("%D")

=> Wed Dec 12 12:32:07 UTC 2007
or @object.date.previous_month. This will avoid those issues.

any

7100 years).


Sincerely,

William P.

Although this has been cleared up, there is the further issue related
to just moving a Time object 30.days because of the differences in
month length. I don’t believe anyone has mentioned the Rails magic of
“next_month” and “previous_month”.

Here is some stuff from console that shows what I’m talking about. The
first set uses Date. You’ll see that adding months doesn’t work but
days does.

set time to current time

time = Time.now.utc
=> Mon Nov 12 12:32:07 UTC 2007

make a date object with the previously set time

x = Date.parse(“#{time}”)
=> #<Date: 4908833/2,0,2299161>
x.strftime(“%D”)
=> “11/12/07”

attempt to add 1.months to x

y = x + 1.months
=> #<Date: 10092833/2,0,2299161>
y.strftime(“%D”)
=> “07/09/04”

attempt to add 30 to x

this works because it is expecting days.

this could work for now, but the time calculations you’ll have to

perform later make this a hassle as you might have been finding out

z = x + 30
=> #<Date: 4908893/2,0,2299161>
z.strftime(“%D”)
=> “12/12/07”


using time objects

set a to current time

a = Time.parse(“#{time}”)
=> Mon Nov 12 12:32:07 UTC 2007

add 1.months to a, it works no problem

b = a + 1.months
=> Wed Dec 12 12:32:07 UTC 2007
b.strftime(“%D”)
=> “12/12/07”

add 30.days to a

this also works no problem.

c = a + 30.days
=> Wed Dec 12 12:32:07 UTC 2007
c.strftime(“%D”)
=> “12/12/07”

Having said that, you’ll still run into issues when you are adding
months or days because of the different number of days in a given
month varies. Your best option is to use @object.date_due.next_month
or @object.date.previous_month. This will avoid those issues.

Here is an example. As you can see, January 31st + 1.months results in
March. January 31st + 30.days is also in March. However, January
31st.next_month is Feb 28th.

a = Time.parse(“01/31/2007”)
=> Wed Jan 31 00:00:00 EST 2007
b = a + 1.months
=> Fri Mar 02 00:00:00 EST 2007
c = a + 30.days
=> Fri Mar 02 00:00:00 EST 2007
d = a.next_month
=> Wed Feb 28 00:00:00 EST 2007

You have to be very careful with time. Also, I might add to avoid any
issue with Daylight Savings Time, you should be using UTC as your time
based an adjust for your local time zone.

On Nov 12, 10:40 am, Frederick C. [email protected]