Get timestamp in javascript

Sometimes you need operate Unix timestamp, so there is how you can get it:

Date.prototype.getTimestamp = function() {
    return parseInt((this.getTime()-Date.UTC(1970,0,1))/1000);
}

Note: If you’d like to use timestamp with milliseconds, just don’t divide by 1000 in above method.
After that for example you are able to use this method to obtain current timestamp:

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

and that’s it, cheers.

Update:
There is another way to get timestamp if you use jQuery:

alert($.now());

You can find detailed info about this method here.

Books to read

Leave a Reply

Your email address will not be published. Required fields are marked *