ES6 Function result is showing NaN










-2















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 '18 at 3:12















-2















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 '18 at 3:12













-2












-2








-2








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 '18 at 3:10









Niran YousufNiran Yousuf

559




559












  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date

    – tkausl
    Nov 11 '18 at 3:12

















  • Have you tried debugging this? console.log(niran.birthday) -> Invalid Date

    – tkausl
    Nov 11 '18 at 3:12
















Have you tried debugging this? console.log(niran.birthday) -> Invalid Date

– tkausl
Nov 11 '18 at 3:12





Have you tried debugging this? console.log(niran.birthday) -> Invalid Date

– tkausl
Nov 11 '18 at 3:12












3 Answers
3






active

oldest

votes


















2














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






























    1














    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






























      1














      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',
        autoActivateHeartbeat: false,
        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









        2














        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



























          2














          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

























            2












            2








            2







            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 '18 at 3:27









            Deepak S.MDeepak S.M

            214




            214























                1














                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



























                  1














                  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

























                    1












                    1








                    1







                    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 '18 at 3:21









                    GigiGigi

                    956




                    956





















                        1














                        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



























                          1














                          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

























                            1












                            1








                            1







                            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 '18 at 4:35









                            ma_dev_15ma_dev_15

                            412211




                            412211



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                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

                                𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                Crossroads (UK TV series)

                                ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế