{"id":1880,"date":"2018-04-27T21:48:56","date_gmt":"2018-04-28T02:48:56","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1880"},"modified":"2018-04-27T21:48:56","modified_gmt":"2018-04-28T02:48:56","slug":"javascript-compare-dates","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/27\/javascript-compare-dates\/","title":{"rendered":"JavaScript: How to compare dates"},"content":{"rendered":"<p>For an explanation of how to get an ISO date-time string, see this previous article:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"SjE9FxPMEo\"><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=SjE9FxPMEo\" data-secret=\"SjE9FxPMEo\" 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>Let&#8217;s say we have two ISO date-time strings that we pulled from a MySQL database and we want to programmatically compare them to see which date is newer. Let&#8217;s set them up as d1 an d2:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let d1 = '2018-04-02T19:23:15.964Z';\r\nlet d2 = '2016-09-14T19:23:15.964Z';<\/pre>\n<p>&nbsp;<\/p>\n<p>Since these came from the database as strings, in order to compare them we first need to convert them into JavaScript Date() objects. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">d1 = new Date(d1);\r\nd2 = new Date(d2);<\/pre>\n<p>&nbsp;<\/p>\n<p>Now that both ISO date-time strings have been converted into Date objects we\u00a0 can compare them directly with traditional greater than-less than operators. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let newer = (d1 &gt; d2);\r\nconsole.log(\"d1 is newer?:\", newer)<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">d1 is newer?: true<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For an explanation of how to get an ISO date-time string, see this previous article: JavaScript: How to create a formatted date time string Let&#8217;s say we have two ISO date-time strings that we pulled from a MySQL database and we want to programmatically compare them to see which date is newer. Let&#8217;s set them &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/27\/javascript-compare-dates\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to compare dates<\/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,45],"class_list":["post-1880","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-date","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1880","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=1880"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1880\/revisions"}],"predecessor-version":[{"id":1884,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1880\/revisions\/1884"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}