{
  "version": "https://jsonfeed.org/version/1",
  "title": "Ian's Digital Garden",
  "home_page_url": "https://ianwwagner.com/",
  "feed_url": "https://ianwwagner.com//tag-javascript.json",
  "description": "",
  "items": [
    {
      "id": "https://ianwwagner.com//date-insanity-with-javascript.html",
      "url": "https://ianwwagner.com//date-insanity-with-javascript.html",
      "title": "Date Insanity with JavaScript",
      "content_html": "<p>Earlier today, a friend sent me a joke about boycotting &lt;some big evil tech company&gt; for three days:\nFeb 29, 30, and 31.\nI thought this was pretty funny.\nBut then, darker thoughts started to form.</p>\n<p>I've recently had the misfortune of doing far too much (read: any) work that involves JavaScript.\nIf you haven't seen <a href=\"https://www.destroyallsoftware.com/talks/wat\">Gary Bernhardt's classic Wat talk</a>,\ngo watch that first.\nIt's less than 5 minutes long, and you'll probably need another 5 after to recover 🤣</p>\n<p>That brings us to today's TIL.\nUpon receiving said joke involving nonexistent dates in February,\nand also being vaguly aware of <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal\">some new API designed to replace it</a>,\nI decided to try this is JavaScript in my browser (Chromium-based,\nso the results would probably apply to Node.js, Deno, and others).</p>\n<pre><code class=\"language-javascript\">new Date('2025-02-29')\n// Sat Mar 01 2025 09:00:00 GMT+0900 (Korean Standard Time)\n</code></pre>\n<p>Classic!\nAnd lest you say this is a bug, here's what <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date\">MDN</a> has to say about this:</p>\n<blockquote>\n<p>Non-standard strings can be parsed in any way as desired by the implementation,\nincluding the time zone — most implementations use the local time zone by default.\nImplementations are not required to return invalid date for out-of-bounds date components,\nalthough they usually do. A string may have in-bounds date components (with the bounds defined above),\nbut does not represent a date in reality (for example, &quot;February 30&quot;).\nImplementations behave inconsistently in this case.\nThe <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#examples\"><code>Date.parse()</code></a> page offers more examples about these non-standard cases.</p>\n</blockquote>\n<p>I'm not aware of any sane language that does this.\nI checked a handful, including Rust, Swift, Python, and golang.\nAll returned an error, threw an exception, or something similar.</p>\n<p>My first assumption was that this was just another case of JS always preferring to do anything besides report an error.\nBut it turns out my browser actually does return <code>Invalid Date</code>\nfor &quot;day&quot; values greater than 31.\nI can only imagine what went into the first implementation of this,\nand is now set in stone for backward compatibility!</p>\n<p>One final factoid. You may reasonably guess that the input to the date constructor is ISO 8601, but that's not quite true.\nIt's a <a href=\"https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format\">simplification of it</a>,\nand it also accepts partial input!</p>\n",
      "summary": "",
      "date_published": "2025-02-28T00:00:00-00:00",
      "image": "",
      "authors": [
        {
          "name": "Ian Wagner",
          "url": "https://fosstodon.org/@ianthetechie",
          "avatar": "media/avi.jpeg"
        }
      ],
      "tags": [
        "javascript"
      ],
      "language": "en"
    }
  ]
}