Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Created January 17, 2018 17:57
Show Gist options
  • Save samoshkin/205a3319d265a9cda2653f76c1926c37 to your computer and use it in GitHub Desktop.
Save samoshkin/205a3319d265a9cda2653f76c1926c37 to your computer and use it in GitHub Desktop.
Date assumes string conversion as a default
let d = new Date();
// get string representation
let str = d.toString(); // 'Wed Jan 17 2018 16:15:42'
// get numeric representation, num of milliseconds since Unix epoch
let num = d.valueOf(); // 1516198542525
// compare with a string representation
// true because d is converted to same string
console.log(d == str); // true
// compare with numeric representation
// false, because d is not converted to a number via valueOf()
console.log(d == num); // false
// Result is 'Wed Jan 17 2018 16:15:42Wed Jan 17 2018 16:15:42'
// '+' same to '==' triggers default conversion mode
console.log(d + d);
// Result is 0, since '-' operator explicitly triggers numeric conversion, not a default one
console.log(d - d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment