{"id":6367,"date":"2020-02-23T01:20:02","date_gmt":"2020-02-23T09:20:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6367"},"modified":"2020-02-23T01:20:48","modified_gmt":"2020-02-23T09:20:48","slug":"leetcode-1360-number-of-days-between-two-dates","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1360-number-of-days-between-two-dates\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1360. Number of Days Between Two Dates"},"content":{"rendered":"\n<p>Write a program to count the number of days between two dates.<\/p>\n\n\n\n<p>The two dates are given as strings, their format is&nbsp;<code>YYYY-MM-DD<\/code>&nbsp;as shown in the examples.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date1 = \"2019-06-29\", date2 = \"2019-06-30\"\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date1 = \"2020-01-15\", date2 = \"2019-12-31\"\n<strong>Output:<\/strong> 15\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The given dates are valid&nbsp;dates between the years&nbsp;<code>1971<\/code>&nbsp;and&nbsp;<code>2100<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Convert to days since epoch<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int daysBetweenDates(string date1, string date2) {\n    array<int, 12> m{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n    auto isLeap = [](int year) {\n      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);\n    };\n    auto daysFromEpoch = [&](const string& date) {\n      int year = stoi(date.substr(0, 4));\n      int month = stoi(date.substr(5, 2));\n      int days = stoi(date.substr(8, 2));      \n      for (int i = 1970; i < year; ++i)\n        days += 365 + isLeap(i);\n      for (int i = 1; i < month; ++i)\n        days += m[i - 1];\n      days += month > 2 && isLeap(year);\n      return days;\n    };\n    return abs(daysFromEpoch(date1) - daysFromEpoch(date2));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write a program to count the number of days between two dates. The two dates are given as strings, their format is&nbsp;YYYY-MM-DD&nbsp;as shown in the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[495,222],"class_list":["post-6367","post","type-post","status-publish","format-standard","hentry","category-string","tag-date","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6367"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6367\/revisions"}],"predecessor-version":[{"id":6369,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6367\/revisions\/6369"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}