11 Replies Latest reply on Nov 17, 2013 10:04 AM by Swiper2727272

# REQ:  script to calculate # of months

Looking for a script that will calculate # of months between 2 dates ( to 2 decimals points )

or if there is a way to make a calculation of a number to # of months

for example:  if i go to google and say 75 days - months  , it returns a value of 2.46

this is what i am looking for

can someone assist ?

with either a date calculation or a number calculation ?

i have tried # of days / 365 x  12 but doesn't do it to decimal points.  just keeps rounding up/down

• ###### 1. Re: REQ:  script to calculate # of months

This is a somewhat odd calculation. What's 1 month? 30 days? 31 days? 29 days? 28 days? 30.5 days?

Anyway, the result of your calculation should produce a number with fractions. You should make sure that your field is not set to show 0 decimal digits.

• ###### 2. Re: REQ:  script to calculate # of months

yes, i have that field set to show 2 decimal places, but still only shows 2.00 or 3.00  etc.

and yes, it is a somewhat odd one, but i'm pretty somone has conquured this type of calculation using javascript before

was just hoping for a helping hand

• ###### 3. Re: REQ:  script to calculate # of months

As noted the days in a month are not constant across the year and vary form 28 -31 days. How are you computing the decimal value of month?

A year does not have 365 days, If it did then there would be no need for leap years and leap centries.

Computing a variable with the result of one calculation method does have the decimal value.

cDateFormat = "d-mmm-yyyy";
var cStartDate = "01-Jan-2000";
var oStartDate = util.scand(cDateFormat, cStartDate);
oStartDate.setHours(0, 0, 0, 000);
var cEndDate = "15-Jan-2000";
var oEndDate = util.scand(cDateFormat, cEndDate);
oEndDate.setHours(0, 0, 0, 000);
var nDiff = oEndDate.getTime() - oStartDate.getTime();
console.println("End date: " + cEndDate);
console.println("Start date: " + cStartDate);
console.println("Difference in milliseconds: " + nDiff);
console.println("Difference in seconds: " + nDiff / 1000);
console.println("Difference in minutes: " + nDiff / (1000 * 60));
console.println("Difference in hours: " + nDiff / (1000 * 60 * 60));
console.println("Difference in days: " + nDiff / (1000 *60 * 60 * 24));
console.println("Difference in weeks: " + nDiff / (1000 *60 * 60 * 24 * 7));
console.println("Difference in years: " + nDiff / (1000 *60 * 60 * 24 * 365.2524));
console.println("Difference in Months: " + nDiff / ((1000 *60 * 60 * 24 * 365.2524) / 12));  // 12 months in a year;

End date: 15-Jan-2000
Start date: 01-Jan-2000
Difference in milliseconds: 1209600000
Difference in seconds: 1209600
Difference in minutes: 20160
Difference in hours: 336
Difference in days: 14
Difference in weeks: 2
Difference in years: 0.0383296591617194
Difference in Months: 0.45995590994063273

Many scirpts turncate the decimal values because keeping them might not meet the need of specific calculation. You are aware that there are methods to roud up to the next iniger, round from .5 up, and turnacate to the lower intiger.

Depending upon the starting date the values can be different:

For a starting date of 1-Feb-2000, 75 days would be  1.810 months but for a starting date of 1-Feb-2001 75 days becomes 1.838 since the number of days in February changes from 29 to 28 and the number of days for the fractional month varies from 26 to 27.

• ###### 4. Re: REQ:  script to calculate # of months

The calculation itself is trivial... The question is what are you calculating, exactly.

• ###### 5. Re: REQ:  script to calculate # of months

The difference between dates in months is usually calculated as whole calendar months, plus fractions at either end which can be calculated in various contractually agreed ways. Many would be deeply unhappy with a calculation which from 31 Jan 2013 to 1 March 2013 recorded 0.95 months.

• ###### 6. Re: REQ:  script to calculate # of months

To emphasise: before you can create JavaScript you must precisely give your rules of calculation.

• ###### 7. Re: REQ:  script to calculate # of months

my goal is to correctly determine age of person based on their Date of Birth and

whateever other date is entered into another field

(using  mm-dd-yyyy format)

in weeks (which i have )

and

in months

currently, i am using script to calcuate difference in days between the 2 date fields and basing the # of months based on this number

Do you have a different way/script that would be more precise ?

• ###### 8. Re: REQ:  script to calculate # of months

Well, without thinking about it too deeply, I'd expect the age in months

(1) to be quoted only as a whole number, fractions would not be expected because of the lack of precision

(2) to be based on the number of complete month-anniversaries. So that someone born on 12 January would be 0 months old on 12 February, 1 month old on 13 February, etc. You cannot calculate month anniversaries directly from weeks or years, only by looking at actual dates.

• ###### 9. Re: REQ:  script to calculate # of months

so, Should I calculate difference in days between DOB and DATE ?

but how can i get an accurate # of months to 2 decimal points based on a number

?

01/01/2013 - 03/01/2013 would be (technically 2 months) BUT based on a number calculation

it would be more like 1.98 or 1.99   because of the difference in amount of days in feb  and alternate months 30 and 31 etc.

this is all so.........confusing

• ###### 10. Re: REQ:  script to calculate # of months

I think a difference in fractional months is useless/meaningless/misleading. If I saw someone quoting an age in fractional months I would mistrust the data.

I would give the age for the case quoted (mm/dd/yy) as 2 months. Nothing else.

• ###### 11. Re: REQ:  script to calculate # of months

I hear what you are saying....but

if i enter in dob and whatever date and the difference is say, 75 days

this can be construed as being possible 2 months, 2.5 months or even 3 months depending on how it is being rounded off

what would be best way to do this calculation?

much of the info i am trying to capture is 'baby' age

so, its kind of important to indicate age to closest number possible based on month

2.5 months would be more accurate than 2 or 3

if you  know what i mean

so, being useless/meaningless is not necessary the case, in my circumstance

thanks for the help  btw!!!