Instead of simply advancing date_due a month from it’s present date
(2007-11-11) it advances it 7 millennia into the future (9104-07-08).
Given the rate the dollar’s falling, my clients probably can’t afford to
wait that long to collect payments. I’m guessing there’s something wrong
with the date formats I’m trying to add. Any idea how I could fix this?
Thanks guys. The “+ 30.days” was indeed the problem. “+ 30”, as you
suggested Bill, took care of that. Ideally I would like to advance
date_due in terms of months (not sure if that’s practical) and limit the
initial date_due to between the 1st and the 28th of a month. Anyone know
of a clean way to do this?
Thanks again for the help guys. This will work for now anyway
Yep, I was just writing up another email about the year bug I added. I
do something similar when generating saved reports using date ranges.
I use mysql’s built in date functions like this one:
I don’t actually use it with update_attributes, but rather with a
select, but this should work. Note that update skips any validation or
other callbacks, but if that doesn’t matter and you don’t care that
this ties you to mysql, this will do what you want. There are bound to
be other ways, but this is pretty easy.
Yep, I was just writing up another email about the year bug I added. I
do something similar when generating saved reports using date ranges.
I use mysql’s built in date functions like this one:
I don’t actually use it with update_attributes, but rather with a
select, but this should work. Note that update skips any validation or
other callbacks, but if that doesn’t matter and you don’t care that
this ties you to mysql, this will do what you want. There are bound to
be other ways, but this is pretty easy.
-Bill
Nice Bill. I’ll play around with that. Thanks for your continued support
and advice. Because of people like you, my first rails app (which is far
too large and ambitious) is coming along great.
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.
Thank you Mr.William P. for sharing that great link with us. I have
been reading your posts and hope you will continue this good work of
replying the post in coming days
Thanks, thats great. I knew there had to be a way to do it, but I have
missed that operator when looking at the docs. Thanks again.
-Bill
Frederick C. wrote:
@client.update_attribute(:date_due, Date.new(old_due.year + 1, 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.
Thanks guys. The “+ 30.days” was indeed the problem. “+ 30”, as you
suggested Bill, took care of that. Ideally I would like to advance
date_due in terms of months (not sure if that’s practical) and limit the
initial date_due to between the 1st and the 28th of a month. Anyone know
of a clean way to do this?
Thanks again for the help guys. This will work for now anyway
Try new_date_due = @client.date_due >> 1
Date#+ takes a number of days as the argument.
Date#>> returns a new date “right shifted” some number of months.
use Date#<< to get a date some number of months ago.
Return a new Date object that is n months later than the current
one.
If the day-of-the-month of the current Date is greater than the
last day of the target month, the day-of-the-month of the returned
Date will be the last day of the target month.
Return a new Date object that is n months earlier than the current
one.
If the day-of-the-month of the current Date is greater than the
last day of the target month, the day-of-the-month of the returned
Date will be the last day of the target month.
Return a new Date object that is n days later than the current one.
n may be a negative value, in which case the new Date is earlier
than the current one; however, #-() might be more intuitive.
If n is not a Numeric, a TypeError will be thrown. In particular,
two Dates cannot be added to each other.
It looks like my problem is in the “+ 30.days” part as this code
returns
as 9104-07-08 in my view.
That is definitely the problem
30.days evaluates to the number of seconds in 30 days (ie 30*86400 =
2592000)
This is great when you’re working with an instance of Time because +
is used to mean ‘add this many seconds’. However, for Date, + means
‘add this many days’.
So Date.new(2007,11,12) + 30.days is actually add 2592000 days (around
7100 years).