{"id":70,"date":"2013-04-10T16:04:02","date_gmt":"2013-04-10T22:04:02","guid":{"rendered":"http:\/\/laubsterboy.com\/blog\/?p=70"},"modified":"2013-04-10T16:04:02","modified_gmt":"2013-04-10T22:04:02","slug":"using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date","status":"publish","type":"post","link":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/","title":{"rendered":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date"},"content":{"rendered":"<p>I recently came across the need to determine how many days fell between the beginning of the year and a given date. The most convenient way to do this is to add a new method to the Date object. Let&#8217;s get started!<\/p>\n<pre class=\"theme:twilight lang:default decode:true\">Date.prototype.getDOY = function() {\n    var januaryFirst = new Date(this.getFullYear(),0,1);\n    return Math.ceil((this - januaryFirst) \/ 86400000);\n}<\/pre>\n<p>Looking at the above code, line one allows us to add a new method to the Date object by adding a new function to its prototype. This will allow the getDOY method to be invoked on any Date object, such as myDateObject.getDOY(). The getDOY function name stands for &#8220;get day of year&#8221; and will return the day of year between 1 and 365. The way this works is by first creating a new Date object with the current year, January for the month, and 1 for the day. Next, the Date object (referenced by the keyword <em><strong>this<\/strong><\/em>) is equal to the amount of milliseconds since January 1, 1970 to present time, and subtracting the amount of milliseconds from January 1, 1970 to January 1 of the current year will leave you with the amount of milliseconds from the beginning of the year to the current date and time. The only thing left to be done is to convert the milliseconds to days, which can be done by dividing the number by 86,400,000 (1000 milliseconds\u00a0 x\u00a0 60 seconds\u00a0 x\u00a0 60 minutes\u00a0 x\u00a0 24 hours).<\/p>\n<p>There you have it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently came across the need to determine how many days fell between the beginning of the year and a given date. The most convenient way to do this is to add a new method to the Date object. Let&#8217;s get started! Date.prototype.getDOY = function() { var januaryFirst = new Date(this.getFullYear(),0,1); return Math.ceil((this &#8211; januaryFirst) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog\" \/>\n<meta property=\"og:description\" content=\"I recently came across the need to determine how many days fell between the beginning of the year and a given date. The most convenient way to do this is to add a new method to the Date object. Let&#8217;s get started! Date.prototype.getDOY = function() { var januaryFirst = new Date(this.getFullYear(),0,1); return Math.ceil((this - januaryFirst) [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\" \/>\n<meta property=\"og:site_name\" content=\"John Russell Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-04-10T22:04:02+00:00\" \/>\n<meta name=\"author\" content=\"John Russell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:site\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Russell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\"},\"author\":{\"name\":\"John Russell\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"headline\":\"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date\",\"datePublished\":\"2013-04-10T22:04:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\"},\"wordCount\":236,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\",\"name\":\"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\"},\"datePublished\":\"2013-04-10T22:04:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.johnrussell.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/\",\"name\":\"John Russell Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\",\"name\":\"John Russell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"caption\":\"John Russell\"},\"url\":\"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog","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":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/","og_locale":"en_US","og_type":"article","og_title":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog","og_description":"I recently came across the need to determine how many days fell between the beginning of the year and a given date. The most convenient way to do this is to add a new method to the Date object. Let&#8217;s get started! Date.prototype.getDOY = function() { var januaryFirst = new Date(this.getFullYear(),0,1); return Math.ceil((this - januaryFirst) [&hellip;]","og_url":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/","og_site_name":"John Russell Blog","article_published_time":"2013-04-10T22:04:02+00:00","author":"John Russell","twitter_card":"summary_large_image","twitter_creator":"@laubsterboy","twitter_site":"@laubsterboy","twitter_misc":{"Written by":"John Russell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#article","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/"},"author":{"name":"John Russell","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"headline":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date","datePublished":"2013-04-10T22:04:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/"},"wordCount":236,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/","url":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/","name":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date - John Russell Blog","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#website"},"datePublished":"2013-04-10T22:04:02+00:00","author":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"breadcrumb":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.johnrussell.dev\/blog\/2013\/04\/using-javascript-to-determine-the-number-of-days-between-the-beginning-of-the-year-and-a-given-date\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.johnrussell.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"Using JavaScript to Determine the Number of Days Between the Beginning of the Year and a Given Date"}]},{"@type":"WebSite","@id":"https:\/\/www.johnrussell.dev\/blog\/#website","url":"https:\/\/www.johnrussell.dev\/blog\/","name":"John Russell Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c","name":"John Russell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","caption":"John Russell"},"url":"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/"}]}},"_links":{"self":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}