ES6 Function result is showing NaN











up vote
-2
down vote

favorite












I have make a ES6 class function, I can't understand why the console result is showing NaN






class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());












share|improve this question






















  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
    – tkausl
    Nov 11 at 3:12















up vote
-2
down vote

favorite












I have make a ES6 class function, I can't understand why the console result is showing NaN






class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());












share|improve this question






















  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
    – tkausl
    Nov 11 at 3:12













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have make a ES6 class function, I can't understand why the console result is showing NaN






class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());












share|improve this question













I have make a ES6 class function, I can't understand why the console result is showing NaN






class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());








class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());





class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}

greeting() {
return `Hello there, This is ${this.firstName} ${this.lastName}`;
}

calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1995);
}
}

const niran = new Person('Niran', 'Yousuf', '26-12-1992');

console.log(niran.calculateAge());






javascript es6-class






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 3:10









Niran Yousuf

539




539












  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
    – tkausl
    Nov 11 at 3:12


















  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
    – tkausl
    Nov 11 at 3:12
















Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
– tkausl
Nov 11 at 3:12




Have you tried debugging this? console.log(niran.birthday) -> Invalid Date
– tkausl
Nov 11 at 3:12












3 Answers
3






active

oldest

votes

















up vote
1
down vote













The syntax for Date is:



new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);


Also there is no such thing (without a library) as DateInterval like you can in PHP.






share|improve this answer




























    up vote
    1
    down vote













    The problem is in the string that you are sending to the Date object on line 5. Just change the values like new Date(26,12,1992).



    The syntax for Date object is :



    new Date()



    new Date(year, month, day, hours, minutes, seconds, milliseconds)



    new Date(milliseconds)



    new Date(date string)



    Check out the docs at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date






    share|improve this answer




























      up vote
      1
      down vote













      You can fix the date diff logic. The problem lies in your difference and how you are passing the date



      var date1 = new Date("7/13/2010"); // this is your birth year
      var date2 = new Date(); // This is your current date
      var timeDiff = Math.abs(date2.getTime() - date1.getTime());
      var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24 * 365));
      alert(diffDays);


      Rest is all logic to calculate date






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245522%2fes6-function-result-is-showing-nan%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        The syntax for Date is:



        new Date();
        new Date(value);
        new Date(dateString);
        new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);


        Also there is no such thing (without a library) as DateInterval like you can in PHP.






        share|improve this answer

























          up vote
          1
          down vote













          The syntax for Date is:



          new Date();
          new Date(value);
          new Date(dateString);
          new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);


          Also there is no such thing (without a library) as DateInterval like you can in PHP.






          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            The syntax for Date is:



            new Date();
            new Date(value);
            new Date(dateString);
            new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);


            Also there is no such thing (without a library) as DateInterval like you can in PHP.






            share|improve this answer












            The syntax for Date is:



            new Date();
            new Date(value);
            new Date(dateString);
            new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);


            Also there is no such thing (without a library) as DateInterval like you can in PHP.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 3:21









            Gigi

            936




            936
























                up vote
                1
                down vote













                The problem is in the string that you are sending to the Date object on line 5. Just change the values like new Date(26,12,1992).



                The syntax for Date object is :



                new Date()



                new Date(year, month, day, hours, minutes, seconds, milliseconds)



                new Date(milliseconds)



                new Date(date string)



                Check out the docs at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date






                share|improve this answer

























                  up vote
                  1
                  down vote













                  The problem is in the string that you are sending to the Date object on line 5. Just change the values like new Date(26,12,1992).



                  The syntax for Date object is :



                  new Date()



                  new Date(year, month, day, hours, minutes, seconds, milliseconds)



                  new Date(milliseconds)



                  new Date(date string)



                  Check out the docs at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    The problem is in the string that you are sending to the Date object on line 5. Just change the values like new Date(26,12,1992).



                    The syntax for Date object is :



                    new Date()



                    new Date(year, month, day, hours, minutes, seconds, milliseconds)



                    new Date(milliseconds)



                    new Date(date string)



                    Check out the docs at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date






                    share|improve this answer












                    The problem is in the string that you are sending to the Date object on line 5. Just change the values like new Date(26,12,1992).



                    The syntax for Date object is :



                    new Date()



                    new Date(year, month, day, hours, minutes, seconds, milliseconds)



                    new Date(milliseconds)



                    new Date(date string)



                    Check out the docs at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 3:27









                    Deepak S.M

                    114




                    114






















                        up vote
                        1
                        down vote













                        You can fix the date diff logic. The problem lies in your difference and how you are passing the date



                        var date1 = new Date("7/13/2010"); // this is your birth year
                        var date2 = new Date(); // This is your current date
                        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24 * 365));
                        alert(diffDays);


                        Rest is all logic to calculate date






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You can fix the date diff logic. The problem lies in your difference and how you are passing the date



                          var date1 = new Date("7/13/2010"); // this is your birth year
                          var date2 = new Date(); // This is your current date
                          var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24 * 365));
                          alert(diffDays);


                          Rest is all logic to calculate date






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You can fix the date diff logic. The problem lies in your difference and how you are passing the date



                            var date1 = new Date("7/13/2010"); // this is your birth year
                            var date2 = new Date(); // This is your current date
                            var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                            var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24 * 365));
                            alert(diffDays);


                            Rest is all logic to calculate date






                            share|improve this answer












                            You can fix the date diff logic. The problem lies in your difference and how you are passing the date



                            var date1 = new Date("7/13/2010"); // this is your birth year
                            var date2 = new Date(); // This is your current date
                            var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                            var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24 * 365));
                            alert(diffDays);


                            Rest is all logic to calculate date







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 4:35









                            ma_dev_15

                            301111




                            301111






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245522%2fes6-function-result-is-showing-nan%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine