{"id":4123,"date":"2017-03-02T02:03:30","date_gmt":"2017-03-02T07:03:30","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=4123"},"modified":"2021-12-08T12:25:27","modified_gmt":"2021-12-08T17:25:27","slug":"javascript-unix-timestamp","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/","title":{"rendered":"Here are Three Subtle Ways to get a Unix Timestamp in JavaScript"},"content":{"rendered":"\n

Did you know you can “easily<\/em>” get a Unix-like timestamp using JavaScript<\/a> in more than just one way? Everyone has their own reasons as to why they would want to do this, but hey! We are not Judging.<\/p>\n\n\n\n

For those who are lost at this point, a Unix<\/a> timestamp which is also known as POSIX time or epoch time is a system for describing instants in time, or so says Wikipedia<\/a>. It is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.<\/p>\n\n\n\n

This system is widely used on Unix-like operating systems that is why is commonly called a Unix timestamp. It is also important to note that this system does not factor in leap seconds and therefore is not a true representation of UTC.<\/p>\n\n\n\n

To print the time stamp on Unix-like operating systems you can simply type this date +%s in the command line. Now let us see how we can do this in JavaScript as we restrict this to Three examples.<\/p>\n\n\n\n

How to get the Unix Timestamp in JavaScript<\/p>\n\n\n\n

Method 1:<\/strong> If you call:<\/p>\n\n\n\n

new Date();<\/code><\/pre>\n\n\n\n

You get a formatted date representation that looks similar to this:<\/p>\n\n\n\n

Wed Mar 01, 2017, 19:56:30 GMT+0300 (E. Africa Standard Time)<\/strong><\/p>\n\n\n\n

That, unfortunately, is not a timestamp. We can take advantage of a side effect of the JavaScript language<\/a> and force that call to give us the timestamp by prefixing the + unary operator<\/strong> which triggers the valueof<\/code><\/em> method in that Date object. Using this method may not be considered good coding practice but it works nevertheless. With that said, therefore to get the timestamp you can call:<\/p>\n\n\n\n

new Date();<\/code><\/pre>\n\n\n\n

or alternatively or more correctly:<\/p>\n\n\n\n

new Date().valueOf();<\/code><\/pre>\n\n\n\n

new Date().valueOf()<\/code><\/p>\n\n\n\n

To get seconds from milliseconds you can do the following.<\/p>\n\n\n\n

Math.floor(new Date().valueOf() \/ 1000);<\/code><\/pre>\n\n\n\n

Method 2:<\/strong> You can get the timestamp using the now()<\/code> method of the Date<\/em> object as shown below.<\/p>\n\n\n\n

Date.now();<\/code><\/pre>\n\n\n\n

Older browsers (read IE8 and older) will not be able to use this method without a shim such as the one shown here.<\/p>\n\n\n\n

if (!Date.now) {\n Date.now = function() { return new Date().getTime(); }\n }<\/code><\/pre>\n\n\n\n

Now, this displays the UTC timestamp in milliseconds. should you want to display the above in seconds you can process it further as seen here.<\/p>\n\n\n\n

Math.floor(Date.now() \/ 1000);<\/code><\/pre>\n\n\n\n

Method 3:<\/strong> As a side effect of the above method, you will notice there is a third way of doing this without creating another date object. it is simply to call the getTime<\/em> method as shown here.<\/p>\n\n\n\n

new Date().getTime();<\/code><\/pre>\n\n\n\n

To retrieve the seconds from the above you can do this:<\/p>\n\n\n\n

Math.round(new Date().getTime()\/1000);<\/code><\/pre>\n\n\n\n

There you have it. That is the basics of getting the Unix-like UTC timestamp in JavaScript, what you do with it, the possibilities are limitless.<\/p>\n","protected":false},"excerpt":{"rendered":"

Did you know you can “easily” get a Unix-like timestamp using JavaScript in more than just one way? Everyone has their own reasons as to why they would want to…<\/p>\n","protected":false},"author":1,"featured_media":4124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,27,28,16,17],"tags":[106,184,212,320,328,471,472,523,604,635,636,638],"yoast_head":"\nHow can I to get a Unix Timestamp in JavaScript to use on my Website?<\/title>\n<meta name=\"description\" content=\"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can I to get a Unix Timestamp in JavaScript to use on my Website?\" \/>\n<meta property=\"og:description\" content=\"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-02T07:03:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T17:25:27+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"Here are Three Subtle Ways to get a Unix Timestamp in JavaScript\",\"datePublished\":\"2017-03-02T07:03:30+00:00\",\"dateModified\":\"2021-12-08T17:25:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\"},\"wordCount\":425,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg\",\"keywords\":[\"Browsers\",\"Debugging\",\"ECMAScript\",\"Internet\",\"JavaScript\",\"Programming\",\"Programming Languages\",\"Scripting\",\"Unix\",\"Web\",\"Web Applications\",\"Web Development\"],\"articleSection\":[\"Articles\",\"Programming\",\"Software\",\"Technology\",\"Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\",\"url\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\",\"name\":\"How can I to get a Unix Timestamp in JavaScript to use on my Website?\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg\",\"datePublished\":\"2017-03-02T07:03:30+00:00\",\"dateModified\":\"2021-12-08T17:25:27+00:00\",\"description\":\"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Timestamp\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Here are Three Subtle Ways to get a Unix Timestamp in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How can I to get a Unix Timestamp in JavaScript to use on my Website?","description":"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/","og_locale":"en_US","og_type":"article","og_title":"How can I to get a Unix Timestamp in JavaScript to use on my Website?","og_description":"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript","og_url":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2017-03-02T07:03:30+00:00","article_modified_time":"2021-12-08T17:25:27+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"Here are Three Subtle Ways to get a Unix Timestamp in JavaScript","datePublished":"2017-03-02T07:03:30+00:00","dateModified":"2021-12-08T17:25:27+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/"},"wordCount":425,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg","keywords":["Browsers","Debugging","ECMAScript","Internet","JavaScript","Programming","Programming Languages","Scripting","Unix","Web","Web Applications","Web Development"],"articleSection":["Articles","Programming","Software","Technology","Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/","url":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/","name":"How can I to get a Unix Timestamp in JavaScript to use on my Website?","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg","datePublished":"2017-03-02T07:03:30+00:00","dateModified":"2021-12-08T17:25:27+00:00","description":"We all Know about the Unix Timestamp and what it means and how useful it is, so here we show you how to get the same using JavaScript","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/javascript-unix-timestamp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/Timestamp.jpg","width":1200,"height":630,"caption":"Timestamp"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/javascript-unix-timestamp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"Here are Three Subtle Ways to get a Unix Timestamp in JavaScript"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/twitter.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/4123"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=4123"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/4123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/4124"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=4123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=4123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=4123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}