ES6 Function result is showing NaN
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());
javascript es6-class
add a comment |
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());
javascript es6-class
Have you tried debugging this?console.log(niran.birthday)
->Invalid Date
– tkausl
Nov 11 '18 at 3:12
add a comment |
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());
javascript es6-class
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
javascript es6-class
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 11 '18 at 3:27
Deepak S.MDeepak S.M
214
214
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 '18 at 3:21
GigiGigi
956
956
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 11 '18 at 4:35
ma_dev_15ma_dev_15
412211
412211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Have you tried debugging this?
console.log(niran.birthday)
->Invalid Date
– tkausl
Nov 11 '18 at 3:12