Hello, How can I get a timestamp in JavaScript? I need simple, understandable & easy code.
You can create a function in Javascript 1.5 and above using Date.now()
var time = Date.now || function() {
return +new Date;
};
//calling function
time();
To get the Unix timestamp you can use the below code
var UnixTimeStamp = Math.round((new Date()).getTime() / 1000);
the above code convert the current date and time to a UNIX timestamp, getTime() returns milliseconds from the UNIX epoch, so divide it by 1000 to get the seconds representation. It is rounded using Math.round() to make it a whole number.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly