Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 969
DanielX2010 Avatar asked May 20 '13 15:05

DanielX2010


People also ask

Does Unix timestamp have milliseconds?

Bookmark this question. Show activity on this post. I have a list of unix timestamps that all contain milliseconds -- they are 13 digits long.

How do you convert timestamps to milliseconds?

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.

How do you convert milliseconds to Date format?

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.

How do you read epoch time in milliseconds?

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.


1 Answers

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.

like image 156
Benjamin Gruenbaum Avatar answered Sep 30 '22 10:09

Benjamin Gruenbaum