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());
javascript es6-class
add a comment |
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());
javascript es6-class
Have you tried debugging this?console.log(niran.birthday)
->Invalid Date
– tkausl
Nov 11 at 3:12
add a comment |
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());
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 at 3:10
Niran Yousuf
539
539
Have you tried debugging this?console.log(niran.birthday)
->Invalid Date
– tkausl
Nov 11 at 3:12
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 at 3:21
Gigi
936
936
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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 at 3:27
Deepak S.M
114
114
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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 at 4:35
ma_dev_15
301111
301111
add a comment |
add a comment |
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 at 3:12