How to get Timestamp in Javascript?

Asked

Viewed 28,585 times

23

I’d like to know how I do in JavaScript to obtain the Timestamp? A number that represents the current date and time. I know we got the object for date and time through:

var d = new Date();

But I don’t know how to proceed from there.

(Question asked in the beta period of Stack Overflow en)

  • 2

    What would that be Timestamp? Remember that different systems have different ways of representing a date by a number. One of the most known is "Unix time" (32-bit or 64-bit), but there are others (Python for example uses a float instead of an integer). Verify that the format used by Javascript is the same as you need for your application. And if applicable, don’t forget to pay attention to the time zone.

6 answers

28


You can use the getTime() of the object Date:

// Pegar do horário atual
var timestamp = new Date().getTime();

// Pegar de uma data específica
var timestamp = new Date(2013, 11, 17).getTime();

But be aware because this date/time is provided by the client operating system, if you need any security or the information goes to the base, use some method in your application on the server(back-end).

UPDATE:

An important detail that went unnoticed by me - and it seems by everyone - is that the parameter Month of the object builder Date is indexed by zero(zero-Indexed), ie starts counting from zero, then: 0 = January and 11 = December. In the example above, I used the number 12 to extract today’s date(17/12/13) but as December is 11, the result of the example is Fri Jan 17 2014 00:00:00 GMT-0200 (Horário brasileiro de verão), that is to say, January, because he throws the date forward - or backward - by doing the calculation. But that’s another story.

  • 2

    +1 for the observation about the month. This always happens in Javascript and also in Java. But I suggest editing the question and not leaving the 12 on top, because a hasty reader can copy the code and not read the update.

  • @utluiz good idea, changed the code.

  • 1

    @utluiz the answers that do not mention this observation deserve upvote since they can reproduce a wrong result?

  • @I don’t see a problem. Having an extra piece of information is good, but not totally necessary, unless, of course, this makes the wrong answer or leads the reader into error.

  • utluiz I would particularly do what you said above. rs

5

As everyone said:

new Date().getTime()

Or else even simpler:

Date.now()

4

Just for the record, other than the above:

+new Date

2

You can use

new Date().getTime();

1

With pure javascript it is possible to get the timestamp like this:

console.log(new Date().getTime());
//ou
alert(new Date().getTime());

0

Short and thick:

+ new Date()

An operator + fires the method valueOf in the object Date and returns the timestamp (without any change).

Details:

In almost all current browsers, you can use Date.now() to obtain the UTC timestamp in millisecond; a notable exception is the IE8 and previous (see compatibility table).

To obtain the timestamp in seconds, you can use:

Math.floor(Date.now() / 1000)

Or alternatively, you could use:

Date.now() / 1000 | 0

Which should be slightly faster but also less readable (see also this response).

I would recommend using Date.now () (with compatibility correction). It is a little better because it is shorter and does not create a new object Date. However, if you do not want a maximum & compatibility fix, you can use the "old" method to get the timestamp in millisecond:

new Date().getTime()

Which you can then convert to seconds like this:

Math.round(new Date().getTime()/1000)

And you can also use the method valueOf that we show above:

new Date().valueOf()

Timestamp in milliseconds

var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();

console.log(timeStampInMs, Date.now());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.