{"id":1337,"date":"2017-03-20T19:25:25","date_gmt":"2017-03-20T13:55:25","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1337"},"modified":"2017-03-29T11:33:31","modified_gmt":"2017-03-29T06:03:31","slug":"get-timestamp-javascript","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/get-timestamp-javascript\/","title":{"rendered":"JAVASCRIPT &#8211; How to get a timestamp in JavaScript"},"content":{"rendered":"<p><strong>timestamp in JavaScript:<\/strong><br \/>\nThe Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20timeInMs%20%3D%20Date.now()%3B%0A%09%09var%20timeStamp%20%3D%20Math.floor(Date.now()%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Get a UNIX timestamp with Javascript<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20ts%20%3D%20Math.round((new%20Date()).getTime()%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Return value<\/strong><\/p>\n<p>A Number representing the milliseconds elapsed since the UNIX epoch.<\/p>\n<p><strong>Description<\/strong><\/p>\n<p>Because now() is a static method of Date, we always use it as Date.now().<\/p>\n<p><strong>Usage<\/strong><\/p>\n<p>Date.getUnixTime() returns the Unix epoch time.<br \/>\nDate.now() polyfill for older browsers.<br \/>\nDate.time() is a a C-style helper function that returns the current Unix time.<\/p>\n<p><strong>Example1:<\/strong><\/p>\n<p>Date.getUnixTime() returns the Unix epoch time.<\/p>\n<p>We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20dateTime%20%3D%20Date.now()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>or<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20dateTime%20%3D%20new%20Date().getTime()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>To get unix timestamp of a specific date pass YYYY-MM-DD or YYYY-MM-DDT00:00:00Z as parameter of Date constructor.<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20dateTime%20%3D%20new%20Date(\u20182012-06-08\u2019).getTime()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>we can just add a + sign also when declaring a Date object like below<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20dateTime%20%3D%20%2Bnew%20Date()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 106px;\">[ad type=\u201dbanner\u201d]\n<p>or for specific date<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20dateTime%20%3D%20%2Bnew%20Date(\u20182012-06-08\u2019)%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Example2:<\/strong><\/p>\n<p>Date.now() polyfill for older browsers.<\/p>\n<p>This method was standardized in ECMA-262 5th edition.<br \/>\nEngines which have not been updated to support this method can work around the absence of this method using the following shim:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(!Date.now)%20%7B%0A%20%20Date.now%20%3D%20function%20now()%20%7B%0A%20%20%20%20return%20new%20Date().getTime()%3B%0A%20%20%7D%3B%0A%7D\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Solution<\/strong><\/p>\n<p>To copy & paste the following code at the beginning of your JavaScript:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dDate.prototype.getUnixTime%20%3D%20function()%20%7B%20return%20this.getTime()%2F1000%7C0%20%7D%3B%0Aif(!Date.now)%20Date.now%20%3D%20function()%20%7B%20return%20new%20Date()%3B%20%7D%0ADate.time%20%3D%20function()%20%7B%20return%20Date.now().getUnixTime()%3B%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 106px;\">[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>timestamp in JavaScript: The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. Syntax [pastacode lang=\u201djavascript\u201d manual=\u201dvar%20timeInMs%20%3D%20Date.now()%3B%0A%09%09var%20timeStamp%20%3D%20Math.floor(Date.now()%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] Get a UNIX timestamp with Javascript [pastacode lang=\u201djavascript\u201d manual=\u201dvar%20ts%20%3D%20Math.round((new%20Date()).getTime()%20%2F%201000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] Return value A Number representing the milliseconds elapsed since the UNIX epoch. Description Because [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[2644,2636,2639,2643,2641,2646,2413,2640,2648,2651,2650,2637,2647,2645,2638,2649,2642],"class_list":["post-1337","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-convert-a-date-into-timestamp-in-javascript-code","tag-date-how-do-you-get-a-timestamp-in-javascript","tag-date-now-javascript","tag-easiest-way-to-extract-unix-timestamp-in-js","tag-get-a-unix-timestamp-with-javascript","tag-get-timestamp-javascript","tag-how-do-you-get-a-timestamp-in-javascript","tag-how-to-get-the-correct-unix-timestamp-from-any-date-in-javascript","tag-javascript-date-now-format","tag-javascript-date-now-format-yyyy-mm-dd","tag-javascript-get-current-date-time","tag-javascript-gettime-method","tag-javascript-timestamp-format","tag-javascript-timestamp-to-date","tag-javascript-get-current-timestamp-in-javascript","tag-node-js-timestamp-format","tag-print-out-a-nicely-formatted-timestamp-in-javascript"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1337"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1337\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}