2

As you might know, you can display the time in JavaScript by doing Date() But can you display the Linux Epoch time in JavaScript?

I would like to know because I have a time converter and It would be nice to show the current Epoch time.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Patton Pierce
  • 317
  • 1
  • 2
  • 14
  • 1
    Possible duplicate of [How to get time in milliseconds since the unix epoch in javascript?](http://stackoverflow.com/questions/9575790/how-to-get-time-in-milliseconds-since-the-unix-epoch-in-javascript) – Igor Apr 25 '16 at 11:59
  • Looking at the two answers below, can you clarify if you want the actual Epoch time, as in when the Epoch was, or do you want the time _since_ the Epoch? – Reinstate Monica Cellio Apr 25 '16 at 12:02
  • I want the current Epoch time. – Patton Pierce Apr 25 '16 at 12:08
  • 1
    Thanks - I've deleted my answer then, so as to not confuse the issue. And just so you know, the Epoch is a set time so you're not looking for the "current Epoch time". You're looking for a unix time (timestamp) since the Epoch :) – Reinstate Monica Cellio Apr 25 '16 at 12:13

3 Answers3

4

You have to use the .getTime() method of the Date object.

var d = new Date();
alert(d.getTime());

Epoch is also called timestamp.

Seb33300
  • 7,464
  • 2
  • 40
  • 57
1

Simple and straightforward.

 var milliseconds = new Date().getTime();
0

Older browsers:

(new Date).getTime()

New way:

Date.now()

Returns in milliseconds, so epoch / 1000 | 0 to get seconds.

wilsonzlin
  • 2,154
  • 1
  • 12
  • 22