How do you get a timestamp in JavaScript?
64
How can I get a timestamp in JavaScript?

Something similar to Unix's timestamp, that is, a single number that represents the current time and date. Either as a number or a string.
improve this question | comment
Maya Gaylord Created at: 2013-11-13 17:07:19 UTC By Maya Gaylord
for "string to timestamp javascript" too :-) - Carleton Padberg
This was too meta not to mention, even on such an old post: @Sam152 this page has since become the first result for Javascript timestamp. Bravo! - Dr. Efrain Lowe
funny thing is, this is now the number one hit for "Javascript timestamp" - Emanuel Heathcote
Also first for "Also first for "1st result for "Javascript timestamp""".  Is that going to far? - Laila Lockman
Also first (and only) for => Also first for "Also first for "1st result for "Javascript timestamp""" :) - Mr. Margot Nitzsche
15 Answers
0
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);

0
jQuery provides its own method to get the timestamp:

var timestamp = $.now();


(besides it just implements (new Date).getTime() expression)

REF: http://api.jquery.com/jQuery.now/
0
JavaScript works with the number of milliseconds since the epoch whereas most other languages work with the seconds. You could work with milliseconds but as soon as you pass a value to say PHP, the PHP native functions will probably fail. So to be sure I always use the seconds, not milliseconds.

This will give you a Unix timestamp (in seconds):

var unix = Math.round(+new Date()/1000);


This will give you the milliseconds since the epoch (not Unix timestamp):

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

0
var $time = Date.now || function() {
  return +new Date;
};

$time()

0
var timestamp = Number(new Date()); // current time as number

0
you all do it too complicated. how about simplicity?

Date.now() /1000 |0


to get it working in IE you could do this:

if (typeof Date.now == "undefined") {
    Date.now = function(){return new Date().getTime()};
}

0
Just to add up, here's a function to return a timestamp string in Javascript. 
Example: 15:06:38 PM

function displayTime() {
    var str = "";

    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    var seconds = currentTime.getSeconds()

    if (minutes < 10) {
        minutes = "0" + minutes
    }
    if (seconds < 10) {
        seconds = "0" + seconds
    }
    str += hours + ":" + minutes + ":" + seconds + " ";
    if(hours > 11){
        str += "PM"
    } else {
        str += "AM"
    }
    return str;
}

0
+new Date;


I like it, because it is small.
0
new Date().valueOf()// returns the number of milliseconds since the epoch

0
The Date.getTime() can be very used with a little tweak:


  The value returned by the getTime method is the number of milliseconds
  since 1 January 1970 00:00:00 UTC.


To get the Unix timestamp such as the one returned by PHP time() function, divide this number by 1000, round or floor if necessary:

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

0
Any browsers not supported Date.now, you can use this for get current date time:

currentTime = Date.now() || +new Date()

0
Here is a simple function to generate timestamp in the format: mm/dd/yy hh:mi:ss 

function getTimeStamp() {
    var now = new Date();
    return ((now.getMonth() + 1) + '/' +
            (now.getDate()) + '/' +
             now.getFullYear() + " " +
             now.getHours() + ':' +
             ((now.getMinutes() < 10)
                 ? ("0" + now.getMinutes())
                 : (now.getMinutes())) + ':' +
             ((now.getSeconds() < 10)
                 ? ("0" + now.getSeconds())
                 : (now.getSeconds())));
}

0
This one has a solution : which converts unixtime stamp to tim in js try this

var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();

0
more simpler way:

var timeStamp=event.timestamp || new Date().getTime();

0
The following returns the number of milliseconds since the epoch.

new Date().getTime();

Your Answer