I am describing here getting timestamp in JavaScript. In development,so many times we do the date and time manipulation and If we need to do it client side means we need to use JavaScript.
timestamp just like Unix’s timestamp, a single number that represents the current time and date. Either as a number or a string.
The following returns the number of milliseconds since since 1 January 1970 00:00:00 UTC.
1 | new Date().getTime(); |
Or on browsers that support ES5 (notably not IE8 and earlier), you can use Date.now:
1 | Date.now(); |
which is really for older browsers:
1 2 3 | if (!Date.now) { Date.now = function() { return new Date().getTime(); }; } |
To get the Unix timestamp such as the one returned by PHP time() function, divide this number by 1000, round or floor if necessary:
1 | (new Date()).getTime() / 1000 |
OR
1 | var unix = Math.round(+new Date()/1000); |
jQuery provides its own method to get the timestamp:
1 | var timestamp = $.now(); |