<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ datetime - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ datetime - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 25 Jun 2026 04:44:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/datetime/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Sort Dates Efficiently in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Recently, I was working on a PowerApps Component Framework (PCF) project that required sorting an array of objects by date. The dates were in ISO 8601 format but without a time zone – for example, "20 ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-sort-dates-efficiently-in-javascript/</link>
                <guid isPermaLink="false">6839b592f8650e337982bcba</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ performance ]]>
                    </category>
                
                    <category>
                        <![CDATA[ datetime ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Brandon Wozniewicz ]]>
                </dc:creator>
                <pubDate>Fri, 30 May 2025 13:41:38 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1748612402734/7124a95d-0a33-4ab6-93d2-d94fc354ae12.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Recently, I was working on a PowerApps Component Framework (PCF) project that required sorting an array of objects by date. The dates were in ISO 8601 format but without a time zone – for example, <code>"2025-05-01T15:00:00.00"</code>.</p>
<p>Without much thought, I wrote something similar to:</p>
<pre><code class="language-JavaScript">const sorted = data.sort((a, b) =&gt; {
  return new Date(a.date) - new Date(b.date);
})
</code></pre>
<p>This worked fine on small datasets. But the array I was sorting had nearly <strong>30,000 objects</strong>. On a fast development machine, the performance hit was around <strong>100–150ms</strong> – already noticeable when combined with other UI work. When I tested with <strong>4× CPU throttling</strong> in the browser, the delay increased to nearly <strong>400ms</strong>, which more accurately simulates a lower-end device. That's a reasonable approach to ensure your app still performs well for users on slower machines.</p>
<p>Results in browser:</p>
<pre><code class="language-Bash">sort_with_date_conversion: 397.955078125 ms
</code></pre>
<p>Output with performance throttled by 4x slowdown</p>
<p>In this article, you will learn how to sort dates efficiently in JavaScript. We'll walk through what makes the method above inefficient, as well as a better pattern–especially when dealing with large amounts of data.</p>
<h3 id="heading-table-of-contents">Table of Contents</h3>
<ol>
<li><p><a href="#heading-why-400ms-feels-slow">Why 400ms Feels Slow</a></p>
</li>
<li><p><a href="#heading-setting-up-our-experiment">Setting Up Our Experiment</a></p>
</li>
<li><p><a href="#heading-the-cost-of-date-conversion">The Cost of Date Conversion</a></p>
</li>
<li><p><a href="#heading-the-lexicographical-superpower-of-iso-8601">The Lexicographical Superpower of ISO 8601</a></p>
</li>
<li><p><a href="#heading-what-if-your-dates-arent-iso-format">What If Your Dates Aren't ISO Format?</a></p>
</li>
<li><p><a href="#heading-key-takeaways">Key Takeaways</a></p>
</li>
</ol>
<h2 id="heading-why-400ms-feels-slow">Why 400ms <em>Feels</em> Slow</h2>
<p>According to Jakob Nielsen's classic <em>"Usability Engineering</em>" (1993), delays under 100 milliseconds are perceived as instantaneous. Between 100ms and 1,000ms, users start to notice lag – even if it doesn't require UI feedback. In my case, 400ms felt <strong>choppy</strong>, especially since the PCF component was already handling other tasks. It wasn't going to cut it.</p>
<h2 id="heading-setting-up-our-experiment">Setting Up Our Experiment</h2>
<p>Let's simulate this with a simple experiment that stress tests our sorting. We'll create an array of 100,000 ISO-formatted dates, and <strong>we will simulate a 4x performance slowdown in the browser for all scenarios:</strong></p>
<pre><code class="language-JavaScript">// Create an array of 100,000 ISO-format dates
const isoArray = [];
let currentDate = new Date(2023, 9, 1); // October 1, 2023

for (let i = 0; i &lt; 100000; i++) {
  const year = currentDate.getFullYear();
  const month = String(currentDate.getMonth() + 1).padStart(2, '0');
  const day = String(currentDate.getDate()).padStart(2, '0');

  isoArray.push({ date: `\({year}-\){month}-${day}`, value: i });
  currentDate.setDate(currentDate.getDate() + 1); // advance by one day
}

// Shuffle the array to simulate unsorted input
function shuffle(array) {
  for (let i = array.length - 1; i &gt; 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
}

shuffle(isoArray);
</code></pre>
<h2 id="heading-the-cost-of-date-conversion">The Cost of Date Conversion</h2>
<p>Now, let's sort using the <code>new Date()</code> method, where each new date is instantiated directly inside the sort method.</p>
<pre><code class="language-JavaScript">console.time('sort_with_date_conversion');

// Sorting by converting each string to a Date object on every comparison
const sortedByDate = isoArray.sort((a, b) =&gt; {
  return new Date(a.date) - new Date(b.date);
});

console.timeEnd('sort_with_date_conversion');
</code></pre>
<p>Result in browser:</p>
<pre><code class="language-Bash">sort_with_date_conversion: 1629.466796875 ms
</code></pre>
<p>Sorting 100,000 dates took almost 2 seconds.</p>
<p>Almost 2 seconds. Ouch.</p>
<h2 id="heading-the-lexicographical-superpower-of-iso-8601">The Lexicographical Superpower of ISO 8601</h2>
<p>Here’s the critical realization: <strong>ISO 8601 date strings are already lexicographically sortable</strong>. That means we can skip the <code>Date</code> object entirely:</p>
<pre><code class="language-JavaScript">console.time('sort_by_iso_string');

// Compare strings directly — thanks to ISO 8601 format
const sorted = isoArray.sort((a, b) =&gt; 
  a.date &gt; b.date ? 1 : -1
);

console.timeEnd('sort_by_iso_string');
console.log(sorted.slice(0, 10));
</code></pre>
<p>Output in the console:</p>
<pre><code class="language-Bash">sort_by_iso_string: 10.549072265625 ms
[
  { date: '2023-10-01', value: 0 },
  { date: '2023-10-02', value: 1 },
  { date: '2023-10-03', value: 2 },
  { date: '2023-10-04', value: 3 },
  { date: '2023-10-05', value: 4 },
  { date: '2023-10-06', value: 5 },
  { date: '2023-10-07', value: 6 },
  { date: '2023-10-08', value: 7 },
  { date: '2023-10-09', value: 8 },
  { date: '2023-10-10', value: 9 }
]
</code></pre>
<p>From 1600ms down to ~10ms. That's a 160x speedup.</p>
<p>Why is this faster? Because using new Date() inside .sort() results in creating two new Date objects <strong>per comparison</strong>. With 100,000 items and how sort works internally, that's <strong>millions</strong> of object instantiations. On the other hand, when we sort lexicographically, we are simply sorting strings, which is far less expensive.</p>
<h2 id="heading-what-if-your-dates-arent-iso-format">What If Your Dates <em>Aren't</em> ISO Format?</h2>
<p>Let's say your dates are in <code>MM/DD/YYYY</code> format. Those strings aren't lexicographically sortable, so you'll need to transform them first.</p>
<h3 id="heading-transform-then-sort">Transform <em>then</em> Sort</h3>
<pre><code class="language-JavaScript">console.time('sort_with_iso_conversion_first');

const sortedByISO = mdyArray
  .map((item) =&gt; { // First convert to ISO format
    const [month, day, year] = item.date.split('/');
    return { date: `\({year}-\){month}-${day}`, value: item.value };
  })
  .sort((a, b) =&gt; (a.date &gt; b.date ? 1 : -1)); // then sort

console.timeEnd('sort_with_iso_conversion_first');
</code></pre>
<p>Output:</p>
<pre><code class="language-Bash">sort_with_iso_conversion_first: 58.8779296875 ms
</code></pre>
<p>Still perceived as instantaneous.</p>
<h3 id="heading-retaining-original-objects">Retaining Original Objects</h3>
<p>If you want to keep your original objects (with non-ISO dates), you can use tuples:</p>
<pre><code class="language-JavaScript">console.time('sort_and_preserve_original');

// Create tuples: [sortableDate, originalObject]
const sortedWithOriginal = mdyArray
  .map((item) =&gt; {
    const [month, day, year] = item.date.split('/');
    return [`\({year}-\){month}-${day}`, item]; // return the tuple items
  })
  .sort((a, b) =&gt; a[0] &gt; b[0] ? 1 : -1) // sort based on the first item
  .map(([, item]) =&gt; item); // Return the original object

console.timeEnd('sort_and_preserve_original');
</code></pre>
<p>Output:</p>
<pre><code class="language-Bash">sort_and_preserve_original: 73.733154296875 ms
</code></pre>
<p>Still within the boundaries of being perceived as instantaneous.</p>
<p>The original data is preserved and the performance falls well within what is perceived as instantaneous.</p>
<h2 id="heading-key-takeaways">Key Takeaways</h2>
<ul>
<li><p><strong>Avoid object creation inside .sort()</strong>, especially for large arrays.</p>
</li>
<li><p><strong>ISO 8601 strings are lexicographically sortable.</strong> Use string comparison when you can.</p>
</li>
<li><p>If your date strings aren't sortable, <strong>map them to a sortable form first</strong>, sort, and optionally map them back.</p>
</li>
<li><p>Minor tweaks in sorting can yield <strong>massive performance gains</strong> – especially in UI components or real-time visualizations.</p>
</li>
</ul>
<p>Found this helpful? I write about practical automation, productivity systems, and building smarter workflows — without the jargon. Visit me at <a href="http://brandonwoz.com">brandonwoz.com</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How JavaScript's Temporal Proposal Will Change Date/Time Functions ]]>
                </title>
                <description>
                    <![CDATA[ JavaScript's handling of dates and times has long frustrated developers. The built-in Date object, created in JavaScript’s early days, has numerous limitations and quirks that complicate working with dates and times. Fortunately for us, the Temporal ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-javascripts-temporal-proposal-will-change-datetime-functions/</link>
                <guid isPermaLink="false">67350bbf437f61ba9155860d</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ datetime ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jesse Hall ]]>
                </dc:creator>
                <pubDate>Wed, 13 Nov 2024 20:27:43 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/mRGtYItJRnA/upload/62e08fa08517011b9a4f54e9002b76ca.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>JavaScript's handling of dates and times has long frustrated developers. The built-in <code>Date</code> object, created in JavaScript’s early days, has numerous limitations and quirks that complicate working with dates and times.</p>
<p>Fortunately for us, the <a target="_blank" href="https://github.com/tc39/proposal-temporal">Temporal proposal</a> aims to address these issues by providing a modern, more intuitive API for date and time manipulation.</p>
<p>In this article, we’ll go over some of the challenges of working with <code>date</code>, what the Temporal API is and how it will work, and what you can use in the meantime until Temporal is ready for production use.</p>
<h2 id="heading-current-issues-with-date-in-javascript">Current Issues with <code>Date</code> in JavaScript</h2>
<h3 id="heading-1-mutable-api">1. Mutable API</h3>
<p>The <code>Date</code> object is mutable, which can lead to bugs and unexpected behavior:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Current Date behavior is mutable</span>
<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-string">'January 1, 2024'</span>);
date.setMonth(<span class="hljs-number">1</span>); <span class="hljs-comment">// Modifies the original date object</span>
<span class="hljs-built_in">console</span>.log(date); <span class="hljs-comment">// February 1, 2024</span>

<span class="hljs-comment">// This mutability can lead to bugs when passing dates between functions</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processDate</span>(<span class="hljs-params">date</span>) </span>{
  date.setDate(date.getDate() + <span class="hljs-number">1</span>); <span class="hljs-comment">// Modifies the original date!</span>
  <span class="hljs-keyword">return</span> date;
}
</code></pre>
<h3 id="heading-2-confusing-month-numbering">2. Confusing month numbering</h3>
<p>Months in <code>Date</code> are zero-based (0-11), while days are one-based (1-31):</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Confusing month numbering</span>
<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">2024</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>); <span class="hljs-comment">// January 1, 2024</span>
<span class="hljs-built_in">console</span>.log(date.getMonth()); <span class="hljs-comment">// 0 (January)</span>
</code></pre>
<h3 id="heading-3-limited-time-zone-support">3. Limited time zone support</h3>
<p>The <code>Date</code> object has limited support for time zones and relies heavily on the system's local time zone:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Time zone handling is system-dependent</span>
<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-string">'2024-01-01T00:00:00Z'</span>);
<span class="hljs-built_in">console</span>.log(date.toString()); <span class="hljs-comment">// Will show different results based on system timezone</span>
</code></pre>
<h2 id="heading-what-is-the-temporal-api">What is the Temporal API?</h2>
<p>Temporal is a proposed new JavaScript API that provides a modern solution for working with dates, times, and timestamps. It's currently a Stage 3 proposal, which means it's in the final stages of development but not yet ready for production use.</p>
<p>Key concepts of Temporal:</p>
<ol>
<li><p><strong>Immutable by default</strong>: All Temporal objects are immutable</p>
</li>
<li><p><strong>Clear separation of concerns</strong>: Different objects for different use cases</p>
</li>
<li><p><strong>Explicit time zone handling</strong>: Better support for working with time zones</p>
</li>
<li><p><strong>Consistent indexing</strong>: All units use 1-based numbering</p>
</li>
</ol>
<h2 id="heading-key-features-of-temporal">Key Features of Temporal</h2>
<h3 id="heading-1-different-types-for-different-needs">1. Different types for different needs</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// PlainDate for working with calendar dates</span>
<span class="hljs-keyword">const</span> date = Temporal.PlainDate.from(<span class="hljs-string">'2024-01-01'</span>);

<span class="hljs-comment">// PlainTime for working with wall-clock time</span>
<span class="hljs-keyword">const</span> time = Temporal.PlainTime.from(<span class="hljs-string">'09:00:00'</span>);

<span class="hljs-comment">// ZonedDateTime for working with specific time zones</span>
<span class="hljs-keyword">const</span> zonedDateTime = Temporal.ZonedDateTime.from(<span class="hljs-string">'2024-01-01T09:00:00[America/New_York]'</span>);
</code></pre>
<p>In this example, Temporal provides different object types for different use cases:</p>
<ul>
<li><p><code>PlainDate</code> is used when you only care about calendar dates without time or timezone information. Perfect for birthdays, holidays, and so on.</p>
</li>
<li><p><code>PlainTime</code> handles time independent of any date or timezone. Useful for recurring events like "9 AM daily standup".</p>
</li>
<li><p><code>ZonedDateTime</code> combines date, time, and timezone information for complete timestamp handling. Great for scheduling meetings across timezones.</p>
</li>
</ul>
<p>Each type is purpose-built and immutable, preventing accidental modifications. This clear separation helps developers choose the right tool for their specific needs, unlike the one-size-fits-all <code>Date</code> object that tries to handle everything and often leads to confusion.</p>
<h3 id="heading-2-immutable-operations">2. Immutable operations</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// All operations return new objects instead of modifying the original</span>
<span class="hljs-keyword">const</span> date = Temporal.PlainDate.from(<span class="hljs-string">'2024-01-01'</span>);
<span class="hljs-keyword">const</span> nextMonth = date.add({ <span class="hljs-attr">months</span>: <span class="hljs-number">1</span> }); 
<span class="hljs-built_in">console</span>.log(date.toString()); <span class="hljs-comment">// '2024-01-01' - original unchanged</span>
<span class="hljs-built_in">console</span>.log(nextMonth.toString()); <span class="hljs-comment">// '2024-02-01' - new object</span>
</code></pre>
<p>This example demonstrates how Temporal's immutable design prevents accidental mutations and makes date arithmetic more predictable.</p>
<p>With the current <code>Date</code> API, methods like <code>setMonth()</code> modify the original object, which can lead to bugs when that object is used in multiple places. In contrast, Temporal's methods always return new objects, leaving the original untouched.</p>
<h3 id="heading-3-better-time-zone-support">3. Better time zone support</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// Explicit time zone handling</span>
<span class="hljs-keyword">const</span> nyDateTime = Temporal.ZonedDateTime.from({
  <span class="hljs-attr">timeZone</span>: <span class="hljs-string">'America/New_York'</span>,
  <span class="hljs-attr">year</span>: <span class="hljs-number">2024</span>,
  <span class="hljs-attr">month</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">day</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">hour</span>: <span class="hljs-number">9</span>
});

<span class="hljs-keyword">const</span> tokyoDateTime = nyDateTime.withTimeZone(<span class="hljs-string">'Asia/Tokyo'</span>);
<span class="hljs-built_in">console</span>.log(tokyoDateTime.toString()); <span class="hljs-comment">// '2024-01-01T23:00:00+09:00[Asia/Tokyo]'</span>
</code></pre>
<p>Unlike the current <code>Date</code> API, which often leads to confusion with implicit time zone conversions, Temporal makes time zone operations explicit and straightforward:</p>
<ol>
<li><p>We create a <code>ZonedDateTime</code> object specifically for New York time zone, with all components (year, month, day, hour) clearly specified. This explicit creation prevents any ambiguity about which time zone we're working with.</p>
</li>
<li><p>Using <code>withTimeZone()</code>, we can easily convert times between zones without complex calculations. The conversion from New York to Tokyo time is handled automatically.</p>
</li>
<li><p>The resulting string output includes the full time zone offset (<code>+09:00</code>) and the time zone name (<code>[Asia/Tokyo]</code>), providing complete clarity about the time being represented.</p>
</li>
</ol>
<p>This approach solves many common time zone-related issues developers face today, such as daylight saving time transitions, time zone offset calculations, and the ambiguity of local vs. UTC times. It's particularly valuable for applications that need to handle global scheduling, event coordination across time zones, or any scenario where precise time zone handling is crucial.</p>
<h2 id="heading-comparing-date-intl-and-temporal">Comparing Date, Intl, and Temporal</h2>
<h3 id="heading-current-approach-with-date-and-intl">Current approach with <code>Date</code> and <code>Intl</code>:</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// Current approach using Date and Intl</span>
<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-string">'2024-01-01T09:00:00Z'</span>);
<span class="hljs-keyword">const</span> formatter = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Intl</span>.DateTimeFormat(<span class="hljs-string">'en-US'</span>, {
  <span class="hljs-attr">timeZone</span>: <span class="hljs-string">'America/New_York'</span>,
  <span class="hljs-attr">dateStyle</span>: <span class="hljs-string">'full'</span>,
  <span class="hljs-attr">timeStyle</span>: <span class="hljs-string">'long'</span>
});

<span class="hljs-built_in">console</span>.log(formatter.format(date)); <span class="hljs-comment">// 'Monday, January 1, 2024 at 4:00:00 AM EST'</span>
</code></pre>
<p>With the current approach, we create a UTC timestamp using <code>Date</code>, need a separate <code>Intl.DateTimeFormat</code> object for formatting, handle time zone conversion implicitly during formatting, and have less control over the exact output format. The resulting output shows 4:00 AM EST because we created the date as 09:00 UTC and when formatted in New York time zone. This implicit conversion can be confusing and error-prone.</p>
<h3 id="heading-future-approach-with-temporal">Future approach with Temporal:</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// Future approach using Temporal</span>
<span class="hljs-keyword">const</span> datetime = Temporal.ZonedDateTime.from(<span class="hljs-string">'2024-01-01T09:00:00[America/New_York]'</span>);
<span class="hljs-built_in">console</span>.log(datetime.toLocaleString(<span class="hljs-string">'en-US'</span>, {
  <span class="hljs-attr">weekday</span>: <span class="hljs-string">'long'</span>,
  <span class="hljs-attr">year</span>: <span class="hljs-string">'numeric'</span>,
  <span class="hljs-attr">month</span>: <span class="hljs-string">'long'</span>,
  <span class="hljs-attr">day</span>: <span class="hljs-string">'numeric'</span>,
  <span class="hljs-attr">hour</span>: <span class="hljs-string">'numeric'</span>,
  <span class="hljs-attr">minute</span>: <span class="hljs-string">'2-digit'</span>,
  <span class="hljs-attr">second</span>: <span class="hljs-string">'2-digit'</span>,
  <span class="hljs-attr">timeZoneName</span>: <span class="hljs-string">'short'</span>
})); <span class="hljs-comment">// 'Monday, January 1, 2024 at 9:00:00 AM EST'</span>
</code></pre>
<p>With Temporal, we create a <code>ZonedDateTime</code> with an explicit time zone, and the formatting options are directly integrated into the API. The time (9:00 AM) is exactly what we specified for New York, with no implicit conversions. This makes the code's behavior more predictable and easier to understand.</p>
<p>Arithmetic also becomes more intuitive with this approach.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> nextWeek = datetime.add({ <span class="hljs-attr">weeks</span>: <span class="hljs-number">1</span> });
<span class="hljs-keyword">const</span> duration = datetime.until(nextWeek);
<span class="hljs-built_in">console</span>.log(nextWeek.toPlainDate().toString()); <span class="hljs-comment">// '2024-01-08'</span>
<span class="hljs-built_in">console</span>.log(duration.toString()); <span class="hljs-comment">// 'PT168H'</span>
</code></pre>
<p>In this example, we can see several key advantages of Temporal's approach:</p>
<ol>
<li><p><strong>Explicit Time Zone Handling</strong>: By creating a <code>ZonedDateTime</code> with <code>[America/New_York]</code>, we explicitly state which time zone we're working with. There's no ambiguity about whether the time is UTC, local, or in another time zone.</p>
</li>
<li><p><strong>Integrated Formatting</strong>: The <code>toLocaleString()</code> method provides a clean, unified way to format dates without needing a separate formatter object. All the formatting options are similar to what you'd use with Intl.DateTimeFormat, maintaining familiarity while simplifying the API.</p>
</li>
<li><p><strong>Intuitive Arithmetic</strong>: The <code>add()</code> and <code>until()</code> methods demonstrate how Temporal makes date/time calculations more straightforward:</p>
<ul>
<li><p><code>add({ weeks: 1 })</code> clearly shows we're adding one week</p>
</li>
<li><p><code>until()</code> returns a proper duration object that can be easily understood and manipulated</p>
</li>
<li><p>The resulting duration of 'PT168H' represents a period of time (P) with 168 hours (T168H), following the ISO 8601 duration format</p>
</li>
</ul>
</li>
<li><p><strong>Type Safety</strong>: By having distinct types like <code>ZonedDateTime</code> and <code>PlainDate</code>, Temporal helps prevent common mistakes. The <code>toPlainDate()</code> method explicitly converts to a date-only representation when we don't need time information.</p>
</li>
</ol>
<p>This approach eliminates many of the gotchas and implicit behaviors that make the current <code>Date</code> API problematic, while providing a more powerful and flexible way to work with dates and times.</p>
<h2 id="heading-current-status-and-alternatives">Current Status and Alternatives</h2>
<h3 id="heading-current-status">Current status</h3>
<ul>
<li><p>Temporal is currently at Stage 3 of the TC39 process</p>
</li>
<li><p>It's not yet ready for production use</p>
</li>
<li><p>Browser support is not yet available natively</p>
</li>
</ul>
<h3 id="heading-recommended-alternatives">Recommended alternatives</h3>
<p>Until Temporal becomes widely available, consider using established libraries:</p>
<ol>
<li><p><a target="_blank" href="https://day.js.org/"><strong>Day.js</strong></a></p>
<ul>
<li><p>Lightweight</p>
</li>
<li><p>Good browser support</p>
</li>
<li><p>Good TypeScript support</p>
</li>
<li><p>Extensible with plugins</p>
</li>
<li><p>Has a large community and active development</p>
</li>
</ul>
</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-comment">// Using Day.js as an alternative</span>
<span class="hljs-keyword">import</span> dayjs <span class="hljs-keyword">from</span> <span class="hljs-string">'dayjs'</span>;
<span class="hljs-keyword">import</span> utc <span class="hljs-keyword">from</span> <span class="hljs-string">'dayjs/plugin/utc'</span>;
<span class="hljs-keyword">import</span> timezone <span class="hljs-keyword">from</span> <span class="hljs-string">'dayjs/plugin/timezone'</span>;

dayjs.extend(utc);
dayjs.extend(timezone);

<span class="hljs-keyword">const</span> date = dayjs(<span class="hljs-string">'2024-01-01'</span>).tz(<span class="hljs-string">'America/New_York'</span>);
<span class="hljs-keyword">const</span> nextWeek = date.add(<span class="hljs-number">1</span>, <span class="hljs-string">'week'</span>);
</code></pre>
<p>For a deeper dive into Day.js, check out this article: <a target="_blank" href="https://www.freecodecamp.org/news/javascript-date-time-dayjs/"><strong>JavaScript Dates – How to Use the DayJS Library to work with Date and Time in JS</strong></a></p>
<ol start="2">
<li><p><a target="_blank" href="https://date-fns.org/"><strong>date-fns</strong></a></p>
<ul>
<li><p>Functional programming approach</p>
</li>
<li><p>Tree-shakeable</p>
</li>
<li><p>Good TypeScript support</p>
</li>
</ul>
</li>
<li><p><a target="_blank" href="https://moment.github.io/luxon/"><strong>Luxon</strong></a></p>
<ul>
<li><p>Similar features to Temporal</p>
</li>
<li><p>Immutable by default</p>
</li>
<li><p>Native time zone and Intl support</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-why-wait-for-temporal">Why wait for Temporal?</h3>
<p>While these libraries are good alternatives, Temporal will offer several advantages:</p>
<ol>
<li><p>Native browser support (no additional bundle size)</p>
</li>
<li><p>Standardized API across all JavaScript environments</p>
</li>
<li><p>Better performance as a native implementation</p>
</li>
<li><p>Consistent behavior across all platforms</p>
</li>
</ol>
<p>Until Temporal reaches Stage 4 and has widespread browser support, I would recommend either using the built-in <code>Date</code> and <code>Intl</code> objects or one of the established libraries mentioned above for production applications. But prepare yourself and be ready for Temporal when it’s ready!</p>
<h2 id="heading-thanks-for-reading">Thanks for reading!</h2>
<p>Check out my other content and let me know how I can help you on your journey to becoming a web developer.</p>
<ul>
<li><p><a target="_blank" href="https://youtube.com/codeSTACKr">Subscribe To My YouTube Channel</a></p>
</li>
<li><p>Socials: <a target="_blank" href="https://twitter.com/codeSTACKr">Twitter</a> | <a target="_blank" href="https://www.linkedin.com/in/codeSTACKr/">LinkedIn</a> | <a target="_blank" href="https://instagram.com/codeSTACKr">Instagram</a></p>
</li>
<li><p><a target="_blank" href="https://codestackr.com/">Sign Up For My Newsletter</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
