{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/2009/12/03/javascript-date-to-time/","result":{"data":{"markdownRemark":{"html":"<p>Subtle differences sometimes indicate the proficiency of a programmer.</p>\n<p>Here are three ways to get the current time in JavaScript:</p>\n<pre class=\"grvsc-container default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">var t1 = (new Date()).getTime(); // 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">var t2 = new Date().getTime(); // 2</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">var t3 = (new Date).getTime(); // 3</span></span></code></pre>\n<p>The bracket placement is as much about language awareness as personal taste. In JavaScript, the brackets are optional for a zero-argument constructor: <code>new Date</code> creates a new <code>Date</code> instance without requiring brackets. But due to JavaScript operator precedence, you can’t write <code>new Date.getTime()</code> because the interpreter sees that as trying to call the constructor for a <code>Date.getTime</code> class (cf. <code>MyPackage.MyClass</code>) - in this case the brackets are required for the statement to parse as intended.</p>\n<p>Reflecting on the three versions above: Version 1 doesn’t do anything to show a deep understanding of JavaScript. Version 2 and 3 are sort of interchangeable, but version 3 just edges ahead because the coder’s displayed knowledge to drop the optional brackets.</p>\n<p>Of course, if you want the most compact code possible, you’d write:</p>\n<pre class=\"grvsc-container default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">var t4 = +new Date; // 4</span></span></code></pre>\n<p>This little gem creates a new Date, then coerces it into a number using a unary <code>+</code> operator - and coercing a Date to a number is defined in the language spec to go via <code>valueOf</code> and <code>getTime</code>.</p>\n<p>Checking the character-count: <code>(new Date).getTime()</code> is 20 characters, while <code>+new Date</code> is 9.</p>\n<p>That 11 character saving might come in handy on, say, the new Google home-page, where they currently use <code>(new Date).getTime()</code> <em>seven times</em>.</p>\n<p>Update: the first three versions are all reformatted to <code>new Date().getTime()</code> by <a href=\"https://prettier.io/\">prettier</a></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .default-dark .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","timeToRead":1,"frontmatter":{"title":"JavaScript Date to Time","blurb":"A short post about using parentheses with JavaScript constructors, and compact code to coerce a Date to a unix timestamp","date":"2009-12-03T11:29:14.000Z","modified":"2013-04-13T16:38:27.000Z"}}},"pageContext":{"slug":"/blog/2009/12/03/javascript-date-to-time/","next":{"fields":{"slug":"/blog/motd/"},"frontmatter":{"title":"Motivational MOTD"}},"previous":{"fields":{"slug":"/blog/2009/03/09/all-in-one-cookie-function/"},"frontmatter":{"title":"All-in-one cookie function"}}}},"staticQueryHashes":["1192980692"]}