Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is a difference in node js Date() object values in console.log()?

I am new to node and I was going through Date() object, values and Date format in Node. When I run the below code, I get different output in console.

const date_1 = new Date();
console.log(date_1);
const date_2 = new Date();
console.log(+date_2);
const date_3 = new Date();
console.log('comma:',date_3);
const date_4 = new Date();
console.log('plus:'+date_4);

gives the below output in console?

2018-02-01T06:55:41.327Z
1517468141327
comma: 2018-02-01T06:55:41.327Z
plus:Thu Feb 01 2018 12:25:41 GMT+0530 (India Standard Time)

Can someone let me know what I am missing here in terms of understanding.

like image 438
Shruthi Bhaskar Avatar asked Jul 24 '26 01:07

Shruthi Bhaskar


1 Answers

For the first and third case are displayed as default

new Date().toJSON()

For second case +new Date() unary operator, it's equivalent to:

function(){ return Number(new Date); }

For the fourth case 'plus:'+date_4, the string are concatenated as result the date is equivalent to

'plus:'+date_4.toString() 
like image 182
deepak thomas Avatar answered Jul 26 '26 13:07

deepak thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!