0

I'm trying to create Date object and do some basic operations with it like adding/subtraction, but something goes wrong. When I simply try to initialize that object with my date alert shows me 1970 year and so on (not my date at all).

Code:

function SmartDate(date) {

    this._workDate = new Date(date);

    this.add = function(days, symbol) {
        if(symbol == "d")
        {
            return this.setDate(this.getDate() + days);
        }
        else if(symbol == "m")
        {
            var dY = this.getFullYear();
            var dM = this.getMonth();
            return (dM+12*dY)+(2+12*dY);
        }
    }

    this.substract = function(hours) {
        if(hours > 0)
        {
            var diff = date.getTime() - hours;
            var hours = Math.floor(diff / 1000 / 60 / 60);
            diff -= hours * 1000 * 60 * 60;
            return diff;
        }
        else 
        {
            return date.getTime() - date.getTime();
        }

    }
    this.toDate = function() {
        return date;
    }
}
var a = new SmartDate(2008,7,7);
alert(a._workDate);

1 Answer 1

1

You're passing in 3 arguments - but only defining 1 argument - you need quotes around your arg!

var a = new SmartDate("2008,7,7");

(Your code was trying to execute new Date(2008) - which is invalid, so you get the default date)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.