Answered by:
add over 24 months in a date-field?

Question
-
Hi,
I recently got help with this script to calculate months and add in a date-field, but now realise that it having problem with values over 24 months.
In the months-field we sometimes enter values up to 48 months, and I need the script to calculate how many years and months that is and add that to an excisting date-field.
The script:
var Lev_date = crmForm.all.new_leveransdatum.DataValue;
var months = crmForm.all.new_mnader.DataValue;
var expire = crmForm.all.new_garantiutgr.DataValue;
oDate = Lev_date;if (oDate == null) {
oDate = new Date();
}
var newMonth = oDate.getMonth() + months;
var newYear = oDate.getYear();if(newMonth>11)
{
newYear++;
newMonth = newMonth%11;
}expire = oDate.setMonth(newMonth);
expire = oDate.setYear(newYear);crmForm.all.new_garantiutgr.DataValue = oDate;
Why dosen't the script handle months values over 1 to 2 years?
Please help.
Best regards
/ Marinfloc
Wednesday, June 16, 2010 1:53 PM
Answers
-
Try to change line
newYear++;
to line
newYear = newYear + newMonth / 12;
Truth is opened the prepared mind
My blog (english)
Мой блог (русскоязычный)- Proposed as answer by Oleksandr Klymenko Wednesday, June 16, 2010 3:19 PM
- Marked as answer by Marinfloc Thursday, June 17, 2010 5:17 AM
Wednesday, June 16, 2010 2:00 PMModerator -
Try
newMonth = (newMonth-1)%11;
Oleksandr Klymenko,
My Blog: www.darkaxe.wordpress.com
((newMonth-1)%12)+1- Marked as answer by Marinfloc Thursday, June 17, 2010 5:17 AM
Wednesday, June 16, 2010 6:15 PM
All replies
-
Try to change line
newYear++;
to line
newYear = newYear + newMonth / 12;
Truth is opened the prepared mind
My blog (english)
Мой блог (русскоязычный)- Proposed as answer by Oleksandr Klymenko Wednesday, June 16, 2010 3:19 PM
- Marked as answer by Marinfloc Thursday, June 17, 2010 5:17 AM
Wednesday, June 16, 2010 2:00 PMModerator -
Hi,
It worked! thank you.
Another question thu, why isn't the calculator correct?
ex.
When I want to add 12 months on 2010-01-01 it should be 2011-01-01. But the result is 2011-02-01??
And when I add 24 months on 2010-01-01, the result is 2012-03-01??It always add another month?
How should I formulate the script so it becomes correct?
Thank you!
/ Marinfloc
Wednesday, June 16, 2010 2:21 PM -
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, June 16, 2010 3:05 PM
Wednesday, June 16, 2010 2:42 PM -
Hi,
It worked! thank you.
Another question thu, why isn't the calculator correct?
ex.
When I want to add 12 months on 2010-01-01 it should be 2011-01-01. But the result is 2011-02-01??
And when I add 24 months on 2010-01-01, the result is 2012-03-01??It always add another month?
How should I formulate the script so it becomes correct?
Thank you!
/ Marinfloc
because there are 12 months in a year not 11. your algorithm has to mod 12 and handle the zeros.Wednesday, June 16, 2010 5:22 PM -
Try
newMonth = (newMonth-1)%11;
Oleksandr Klymenko,
My Blog: www.darkaxe.wordpress.com
((newMonth-1)%12)+1- Marked as answer by Marinfloc Thursday, June 17, 2010 5:17 AM
Wednesday, June 16, 2010 6:15 PM -
Hi,
Thank you!
((newMonth-1)%12)+1
Worked perfect!
Thursday, June 17, 2010 5:18 AM