{"id":1256,"date":"2014-01-27T12:04:03","date_gmt":"2014-01-27T18:04:03","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1256"},"modified":"2014-01-27T12:04:31","modified_gmt":"2014-01-27T18:04:31","slug":"code-of-the-day-javascript-date-prototype-setday","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2014\/01\/code-of-the-day-javascript-date-prototype-setday\/","title":{"rendered":"Code of the Day: Javascript Date.prototype.setDay()"},"content":{"rendered":"<p>In Javascript, I find it strange how `Date.prototype.getDay()` in defined but `Date.prototype.setDay()`.<br \/>\nSo here&#8217;s the code for `Date.prototype.setDay()`.<br \/>\n<code><\/p>\n<pre lang=\"javascript\">\r\n\/\/ Sets the Date object to the day of the week.\r\n\/\/ @param {Number} - dayIndex must be from 0 - 6, where 0 is sunday.\r\n\/\/ @return {Number} returns the primitive value of the Date object.\r\nDate.prototype.setDay = Date.prototype.setDay || function (dayIndex) {\r\n\tdayIndex = Math.floor(+dayIndex);\r\n\tif (dayIndex < 0 || 6 < dayIndex) {\r\n\t\tthrow new Error(\"Must pass integer between 0-6, where sunday is 0.\");\r\n\t}\r\n\tthis.setDate( this.getDate() + dayIndex - this.getDay() );\r\n\treturn this.valueOf();\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<h2>Demo 1<\/h2>\n<p><code><\/p>\n<pre lang=\"javascript\">\r\nvar a = new Date(Date.parse(\"Feb 1, 2014\"));\r\na.setDay(0);\r\nconsole.log(a.toDateString() === \"Sun Jan 26 2014\" );\r\na.setDay(6);\r\nconsole.log(a.toDateString() === \"Sat Feb 01 2014\" );\r\n<\/pre>\n<p><\/code><\/p>\n<h2>Demo 2<\/h2>\n<p><code><\/p>\n<pre lang=\"javascript\">\r\n\/\/ Prings the Monday - Friday for the current day.\r\nvar getWeekString = function () {\r\n\tvar days = [],\r\n\t\tdate = new Date();\r\n\t\t\r\n\tfor (var i = 1, len = 6; i < len; i++) {\r\n\t\tdate.setDay(i)\r\n\t\tdays.push(\"## \" + date.toDateString() );\r\n\t}\r\n\treturn days;\r\n};\r\nconsole.log(getWeekString().join(\"\\n\\n\"));\r\n<\/pre>\n<p><\/code><\/p>\n<p>If you looking for a good Javascript Date library, checkout <a href=\"http:\/\/momentjs.com\/\">moment.js<\/a>.<\/p>\n<p>More info:<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/\">MDN Date Object<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Javascript, I find it strange how `Date.prototype.getDay()` in defined but `Date.prototype.setDay()`. So here&#8217;s the code for `Date.prototype.setDay()`. \/\/ Sets the Date object to the day of the week. \/\/ @param {Number} &#8211; dayIndex must be from 0 &#8211; 6, where 0 is sunday. \/\/ @return {Number} returns the primitive value of the Date object. &hellip; <a href=\"https:\/\/bateru.com\/news\/2014\/01\/code-of-the-day-javascript-date-prototype-setday\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript Date.prototype.setDay()<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30,158,164],"class_list":["post-1256","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day","tag-date","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1256"}],"version-history":[{"count":3,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":1260,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1256\/revisions\/1260"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}