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.
Bookmark this question. Show activity on this post. I have a list of unix timestamps that all contain milliseconds -- they are 13 digits long.
You can get the time in seconds using time. time function(as a floating point value). To convert it to milliseconds, you need to multiply it with 1000 and round it off.
Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.
Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds.
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 expressionnew Date(…)
with the same arguments.
This is unlike when using new Date
which does what you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With