Linefeed
Linefeed

Reputation: 979

How to do Year / Month Calculations in Javascript?

We have a Year/Month HTML control. The underlying value is kept as a decimal. For Example: 15 year and 2 months is equal to "15.02".

We want to do calculations based on 2 year/month controls (add/substract).

Just doing basic math calculations doesn't work:

  Value1 : 65.00
  Value2 : 47.09
  65.00 - 14.09 = 17.91 

 (is wrong, but should be 17.03 or (64.12 - 47.09))

Are there any Javscript/Jquery functions for that or a library that I can use to do Year / Month calculations ?

Upvotes: 1

Views: 290

Answers (6)

timidboy
timidboy

Reputation: 1722

var a = '65.00'.split('.');
var b = '47.09'.split('.');
a = a[0] * 12 + Number(a[1]);
b = b[0] * 12 + Number(b[1]);
c = a - b;
c = Math.floor(c / 12) + (c % 12) / 100;

Upvotes: 0

theintersect
theintersect

Reputation: 758

Here you can try this. It basically checks if your numbers are just years or if they contain months. The script is just a template, you can continue it and improve it, create a function out of it, or define your own prototype. Just check the code, hope it helps.

http://jsfiddle.net/6pRGv/

Upvotes: 0

Paul D. Waite
Paul D. Waite

Reputation: 98796

JavaScript doesn’t support operator overloading, so I don’t think you’ll be able to find a solution that allows you to literally use + and - with composite year/month values.

However, you could define your own year/month object type with add and subtract methods:

function YearsMonths(years, months) {
    this.years = years;

    if (months > 11) {
        this.years = this.years += Math.floor(months/12);
        this.months = months % 12;
    }
    else {
        if (months < 0) {
            this.months = 12 + (months % -12);
            this.years -= (Math.floor(months/-12) + 1);
        }
        else {
            this.months = months;
        }
    }
}

YearsMonths.prototype.add = function (otherYearsMonths) {
    newYears = this.years + otherYearsMonths.years;
    newMonths = this.months + otherYearsMonths.months;

    return new YearsMonths(newYears, newMonths);
}

YearsMonths.prototype.subtract = function (otherYearsMonths) {
    var newYears = this.years - otherYearsMonths.years,
        newMonths = this.months - otherYearsMonths.months;

    return new YearsMonths(newYears, newMonths);
}

And then use it like this:

value1 = new YearsMonths(65, 0);
value2 = new YearsMonths(47, 9);

value3 = value1.subtract(value2);
value4 = value1.add(value2);

value3.years;
# 17
value3.months;
# 3

value4.years;
# 112
value4.months;
# 9

Upvotes: 0

Anusha
Anusha

Reputation: 175

The way you are calculating with decimals will not work! coz, JS consider that as a number not as a date..

To calculate Years or Months (basically any time/date system), you require the proper syntax. Visit this for the syntax

Hope this will work for you

Upvotes: 0

nickdos
nickdos

Reputation: 8414

Just convert the month portion to regular decimal (divide by 12), do the maths, then convert the decimal fraction back to months (multiply by 12).

You'll have to parse the bits out with something like var bits = Value1.split(".");. The year would be bits[0] and month bits[1].

E.g. 47.09 would be 47 + (9 / 12 = 0.75) = 47.75.

So 65 - 47.75 = 12.25.

Convert decimal fraction part back to months: 0.25 * 12 = 0.3. So answer is 47.03.

Upvotes: 0

Evan Trimboli
Evan Trimboli

Reputation: 30082

Have a look at DateJS: http://code.google.com/p/datejs/wiki/APIDocumentation

Upvotes: 2

Related Questions