BeCool
BeCool

Reputation: 529

Datetime in javascript, is it a bug?

Hello all javascript guru,

I've came across this javascript code that struggled me abit to understand why it didn't work as expected. So thought i post it here seeking for your help.

Basically the code is to convert a Date to milliseconds format and that milliseconds value then be converted back to Date object again but when comparing, they are NOT equal?

var currentTime = new Date();
var currentTimeInMill = Date.parse(currentTime);
var currentTime2 = new Date(currentTimeInMill);

// debug in firefox
console.log(currentTime);
// Date {Thu Jul 07 2011 09:56:19 GMT+1000 (AUS Eastern Standard Time)}

console.log(currentTime2);
// Date {Thu Jul 07 2011 09:56:19 GMT+1000 (AUS Eastern Standard Time)}

console.log(currentTime == currentTime2);
// false

My question is why that 2 values currentTime and currentTime2 are not equal even their values "look" the same in Firefox console.log?

Upvotes: 2

Views: 835

Answers (4)

kennebec
kennebec

Reputation: 104770

currentTime-currentTime2===0 is a quick way to test thet two dates refer to the same timestamp.

Upvotes: 0

eyelidlessness
eyelidlessness

Reputation: 63519

Because, as minitech points out, milliseconds are stripped from the Date object's toString method, in order to be absolutely sure that the numeric value of currentTime equals that of currentTime2, you'll need to set the milliseconds of currentTime2:

currentTime2.setMilliseconds(currentTime % 1000);

Then, as cwolves points out, you can compare each Date's getTime output. So when complete, it would look something like this:

var currentTime = new Date();
var currentTimeInMill = Date.parse(currentTime);
var currentTime2 = new Date(currentTimeInMill);
currentTime2.setMilliseconds(currentTime % 1000);

// debug in firefox
console.log(currentTime);
// Date {Thu Jul 07 2011 09:56:19 GMT+1000 (AUS Eastern Standard Time)}

console.log(currentTime2);
// Date {Thu Jul 07 2011 09:56:19 GMT+1000 (AUS Eastern Standard Time)}

console.log(currentTime.getTime() == currentTime2.getTime());

Upvotes: 1

Ry-
Ry-

Reputation: 224903

It's because time strings aren't round-trip. If you try this:

var currentTime = new Date();
var currentTimeInMill = Date.parse(currentTime);
var currentTime2 = new Date(currentTimeInMill);

console.log(currentTime.getTime());

console.log(currentTime2.getTime());

console.log(currentTime == currentTime2);
// false

You'll see that the times are different. currentTime2 ends in 3 zeroes and currentTime has something else. It's a minute difference, in the milliseconds, which date strings obviously don't include.

Upvotes: 2

user578895
user578895

Reputation:

You won't ever get two "Objects" in JavaScript to be equal using == unless they reference the exact same object. Javascript equality checks to see if they reference the same object before it checks to see if any values are similar.

Put simply, when comparing objects == is identical to ===

Your check should be:

currentTime.getTime() == currentTime2.getTime();
// or
+currentTime == +currentTime2; // cast both to numbers and compare the numbers

Upvotes: 3

Related Questions