{"id":1870,"date":"2018-04-27T20:46:12","date_gmt":"2018-04-28T01:46:12","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1870"},"modified":"2018-04-27T20:47:47","modified_gmt":"2018-04-28T01:47:47","slug":"javascript-add-subtract-days-iso-date-time-strings","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/27\/javascript-add-subtract-days-iso-date-time-strings\/","title":{"rendered":"JavaScript: How to add and subtract days from ISO date-time strings"},"content":{"rendered":"<p>For an explanation of what an ISO date-time string is, see this previous article:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"USZIafPivH\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/2018\/04\/09\/javascript-create-formatted-date-time-string\/\">JavaScript: How to create a formatted date time string<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/2018\/04\/09\/javascript-create-formatted-date-time-string\/embed\/#?secret=USZIafPivH\" data-secret=\"USZIafPivH\" width=\"600\" height=\"338\" title=\"&#8220;JavaScript: How to create a formatted date time string&#8221; &#8212; Chris Nielsen Code Walk\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Now let&#8217;s say we get today&#8217;s date and we want to also get dates for one week ago and one week in the future. We can get today&#8217;s date in the form of an ISO date-time string like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let todaysDate = new Date().toISOString();<\/pre>\n<p>Here is the code (with comments) for creating a variable called &#8216;sevenDaysAgo&#8217; that is set to today&#8217;s date minus seven days. It uses a temporary variable to transform today&#8217;s date with a combination of\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">.setDate()<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">.getDate()<\/code>, then places that transformed date into the sevenDaysAgo variable. After that, the sevenDaysAgo variable is turned into a Date object and converted into an ISO date-time string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ set up a temporary date object 'd'\r\nlet d = new Date();\r\n\r\n\/\/ use a combination of setDate and getDate +- the number of days\r\n\/\/ on the temporary date object, then asign it to our variable\r\nlet sevenDaysAgo = d.setDate(d.getDate() - 7);\r\n\r\n\/\/ turn our date variable into a new date, and convert it to an ISO date-time string\r\nsevenDaysAgo = new Date(sevenDaysAgo).toISOString();\r\n\r\n\/\/ see the result in the console\r\nconsole.log(sevenDaysAgo)<\/pre>\n<p>Here is the code (without comments) for how to create a sevenDaysFromNow date:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let d = new Date();\r\nlet sevenDaysFromNow = d.setDate(d.getDate() + 7);\r\nsevenDaysFromNow = new Date(sevenDaysFromNow).toISOString();\r\nconsole.log(sevenDaysFromNow)<\/pre>\n<p>&nbsp;<\/p>\n<p>For more information about the \u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">.setDate()<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">.getDate()<\/code> functions, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_setdate.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_setdate.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/setDate\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/setDate<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_getDate.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_getDate.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/getDate\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/getDate<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For an explanation of what an ISO date-time string is, see this previous article: JavaScript: How to create a formatted date time string Now let&#8217;s say we get today&#8217;s date and we want to also get dates for one week ago and one week in the future. We can get today&#8217;s date in the form &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/27\/javascript-add-subtract-days-iso-date-time-strings\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to add and subtract days from ISO date-time strings<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[125,130,45,129,126],"class_list":["post-1870","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-date","tag-getdate","tag-javascript","tag-setdate","tag-toisostring"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":6,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":1878,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1870\/revisions\/1878"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}