DanielX2010
DanielX2010

Reputation: 1908

Conversion from milliseconds to Unix Timestamp from different times gives same result

I have two variables:

tempTimeRequests timeLastUpdateRequests

Both are given in milliseconds since epoch.

I'm facing a bizarre behaviour from js:

the result I get for

alert(
    tempTimeRequests+"\n"+
    timeLastUpdateRequests+"\n"+
    Date(tempTimeRequests)+"\n"+
    Date(timeLastUpdateRequests)
)

is

1369063665000
1369063651000
Mon May 20 2013 17:27:51 GMT+0200 (CEST)
Mon May 20 2013 17:27:51 GMT+0200 (CEST)

How come I have the same value of seconds if clearly have 51 seconds for the second (which gives the right result) but 65 (which would give 05 seconds) for the first ? I am really freaking out with that.

Upvotes: 5

Views: 294

Answers (2)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276286

Calling the Date constructor as a function returns the current date.

From the specification

15.9.2 The Date Constructor Called as a Function

When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC).

NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments.

This is unlike when using new Date which does what you expect.

Upvotes: 4

Robbert
Robbert

Reputation: 6582

This should fix the problem

alert(
  tempTimeRequests+"\n"+
  timeLastUpdateRequests+"\n"+
  new Date(tempTimeRequests)+"\n"+
  new Date(timeLastUpdateRequests) 
)

Upvotes: 3

Related Questions