{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">var timeInMs = Date.now();<br\/>\t\tvar timeStamp = Math.floor(Date.now() \/ 1000);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Get a UNIX timestamp with Javascript<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">var ts = Math.round((new Date()).getTime() \/ 1000);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const dateTime = Date.now();<br\/>const timestamp = Math.floor(dateTime \/ 1000);<\/code><\/pre> <\/div>\n<p>or<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const dateTime = new Date().getTime();<br\/>const timestamp = Math.floor(dateTime \/ 1000);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const dateTime = new Date(&#039;2012-06-08&#039;).getTime();<br\/>const timestamp = Math.floor(dateTime \/ 1000);<\/code><\/pre> <\/div>\n<p>we can just add a + sign also when declaring a Date object like below<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const dateTime = +new Date();<br\/>const timestamp = Math.floor(dateTime \/ 1000);<\/code><\/pre> <\/div>\n<p style=\"top: 106px;\">[ad type=&#8221;banner&#8221;]\n<p>or for specific date<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const dateTime = +new Date(&#039;2012-06-08&#039;);<br\/>const timestamp = Math.floor(dateTime \/ 1000);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">if (!Date.now) {<br\/>  Date.now = function now() {<br\/>    return new Date().getTime();<br\/>  };<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Solution<\/strong><\/p>\n<p>To copy &amp; paste the following code at the beginning of your JavaScript:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">Date.prototype.getUnixTime = function() { return this.getTime()\/1000|0 };<br\/>if(!Date.now) Date.now = function() { return new Date(); }<br\/>Date.time = function() { return Date.now().getUnixTime(); }<\/code><\/pre> <\/div>\n<p style=\"top: 106px;\">[ad type=&#8221;banner&#8221;]\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 [ad type=&#8221;banner&#8221;] Get a UNIX timestamp with Javascript Return value A Number representing the milliseconds elapsed since the UNIX epoch. Description Because now() is a static method of Date, we always use it as Date.now(). Usage [&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}]}}