<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Faiz Ahmad</title>
    <description>The latest articles on Forem by Faiz Ahmad (@faiz_ahmad).</description>
    <link>https://forem.com/faiz_ahmad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2041768%2Fca68a814-0851-4c6b-a504-b738cfc122ed.jpg</url>
      <title>Forem: Faiz Ahmad</title>
      <link>https://forem.com/faiz_ahmad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/faiz_ahmad"/>
    <language>en</language>
    <item>
      <title>Understanding Optimistic Locking: Handling Conflicts in Concurrent Systems</title>
      <dc:creator>Faiz Ahmad</dc:creator>
      <pubDate>Mon, 09 Sep 2024 03:42:44 +0000</pubDate>
      <link>https://forem.com/faiz_ahmad/understanding-optimistic-locking-handling-conflicts-in-concurrent-systems-3ce8</link>
      <guid>https://forem.com/faiz_ahmad/understanding-optimistic-locking-handling-conflicts-in-concurrent-systems-3ce8</guid>
      <description>&lt;p&gt;In modern applications, where multiple users might try to access or update the same data simultaneously, ensuring data consistency while allowing concurrency is critical. One common way to manage this is through &lt;strong&gt;optimistic locking&lt;/strong&gt;. This article will explain optimistic locking, walk through an example scenario, and explore when it’s appropriate to use and when it’s not.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What is Optimistic Locking?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Optimistic locking is a concurrency control mechanism used to manage simultaneous updates to the same data in a database. It assumes that conflicts are rare and allows multiple users to proceed with their operations without locking the data. At the time of committing the changes, optimistic locking checks if any conflict has occurred (i.e., if the data was modified by another user). If a conflict is detected, the transaction fails, and the user must retry.&lt;/p&gt;

&lt;p&gt;Unlike &lt;strong&gt;pessimistic locking&lt;/strong&gt;, which prevents conflicts by locking data during a transaction, optimistic locking allows changes to proceed concurrently but checks for conflicts only when changes are committed.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Use Case for Optimistic Locking&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Optimistic locking is ideal in scenarios where conflicts are unlikely but must still be handled gracefully when they occur. This makes it useful in collaborative applications, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Document editing systems&lt;/strong&gt; (e.g., wikis or blog posts), where multiple users may edit the same document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared shopping carts&lt;/strong&gt;, where multiple users might add or update items simultaneously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content management systems (CMS)&lt;/strong&gt;, where users may update posts or entries without frequent overlapping changes.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Example Scenario: Conflict Detection with Optimistic Locking&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s explore how &lt;strong&gt;optimistic locking&lt;/strong&gt; works in a database by simulating two users trying to update the same document simultaneously. We’ll use MySQL commands to illustrate this scenario.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Initial Setup&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Assume we have a &lt;code&gt;documents&lt;/code&gt; table with a version number that tracks changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;-- Version number for optimistic locking&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We insert an initial document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'My First Document'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'This is the initial content.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, both users will attempt to modify the document simultaneously.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2: Simulating Two Users Editing the Document&lt;/strong&gt;
&lt;/h4&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;User A (First Transaction)&lt;/strong&gt;
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start a Transaction:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;START&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the Document:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User A&lt;/strong&gt; sees the content and the version of the document (&lt;code&gt;version = 1&lt;/code&gt;).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update the Document:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'This is User A’s updated content.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
   &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This update checks that the document’s version is still &lt;code&gt;1&lt;/code&gt;. If so, it updates the content and increments the version number.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Commit the Transaction:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;strong&gt;User A&lt;/strong&gt; has successfully updated the content, and the version number of the document is incremented to &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;User B (Second Transaction)&lt;/strong&gt;
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start a Transaction:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;START&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the Document:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User B&lt;/strong&gt; sees the same version (&lt;code&gt;1&lt;/code&gt;), since &lt;strong&gt;User A&lt;/strong&gt; has not yet committed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Attempt to Update the Document:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'This is User B’s updated content.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
   &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Commit the Transaction:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3: Conflict Detection&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;User B&lt;/strong&gt; tries to commit the update, the database checks the version of the document.&lt;/li&gt;
&lt;li&gt;Since &lt;strong&gt;User A&lt;/strong&gt; has already committed their changes and incremented the version number to &lt;code&gt;2&lt;/code&gt;, the condition &lt;code&gt;WHERE version = 1&lt;/code&gt; fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User B's&lt;/strong&gt; transaction is not completed, and a conflict is detected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, &lt;strong&gt;User B&lt;/strong&gt; can either retry the operation after reloading the updated data or handle the conflict by informing the user.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;When Not to Use Optimistic Locking&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While &lt;strong&gt;optimistic locking&lt;/strong&gt; is great for collaborative environments with low chances of conflicts, there are scenarios where it’s not the best fit:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High Contention Scenarios:&lt;/strong&gt;&lt;br&gt;
In systems where multiple users frequently update the same data, conflicts might be too common, causing frequent transaction failures and retries. In such cases, &lt;strong&gt;pessimistic locking&lt;/strong&gt; is a better fit because it prevents conflicts from happening at all by locking data upfront.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-Time Systems:&lt;/strong&gt;&lt;br&gt;
Applications like financial systems or high-frequency trading platforms, where data integrity and timing are critical, often use &lt;strong&gt;pessimistic locking&lt;/strong&gt; or &lt;strong&gt;other concurrency controls&lt;/strong&gt; to ensure consistency without waiting for conflicts to be detected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Latency-Sensitive Environments:&lt;/strong&gt;&lt;br&gt;
In situations where retrying operations due to conflicts introduces unacceptable latency, optimistic locking might lead to a poor user experience. In these cases, &lt;strong&gt;pessimistic locking&lt;/strong&gt; or &lt;strong&gt;other locking strategies&lt;/strong&gt; might be more suitable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Optimistic locking is a powerful mechanism for handling concurrent transactions, allowing multiple users to work on the same data without locking it, but still detecting conflicts when they occur. It’s especially useful in collaborative environments where the likelihood of conflicts is low, but it may not be ideal for high-contention systems where conflicts are frequent.&lt;/p&gt;

&lt;p&gt;Have you used optimistic locking in your projects? What has your experience been? Share your thoughts in the comments below, and don’t forget to like, share, and provide your feedback on this article!&lt;/p&gt;

</description>
      <category>database</category>
      <category>systemdesign</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>Handling Date and Timezone Conversions: Why Proper UTC Conversion Matters</title>
      <dc:creator>Faiz Ahmad</dc:creator>
      <pubDate>Sun, 08 Sep 2024 07:39:59 +0000</pubDate>
      <link>https://forem.com/faiz_ahmad/handling-date-and-timezone-conversions-why-proper-utc-conversion-matters-40a2</link>
      <guid>https://forem.com/faiz_ahmad/handling-date-and-timezone-conversions-why-proper-utc-conversion-matters-40a2</guid>
      <description>&lt;p&gt;While retrieving data for a selected date range, we noticed that our calculation was off by some margin. However, when we decreased the date by one day, the data matched exactly! &lt;/p&gt;

&lt;p&gt;Hmmm… There might be an issue with how the date is being handled in our code. Perhaps the timezone is not being handled correctly—and yes, I was right!&lt;/p&gt;

&lt;p&gt;When building applications that involve users from different timezones, handling dates properly can be tricky. Storing dates in &lt;strong&gt;UTC&lt;/strong&gt; is a common best practice to ensure consistency, but things can get complicated when users input dates in their local timezone, especially during filtering and querying.&lt;/p&gt;

&lt;p&gt;Developers often resort to the native &lt;strong&gt;JavaScript &lt;code&gt;Date&lt;/code&gt; object&lt;/strong&gt; for handling these conversions. However, this approach can lead to inconsistencies across environments, such as Node.js vs. browser consoles like Chrome. In this article, we’ll explore why handling date and timezone conversions properly is crucial, how &lt;strong&gt;Luxon&lt;/strong&gt; can make this process easier, and why relying on the native JavaScript &lt;code&gt;Date&lt;/code&gt; object can lead to inconsistencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem: UTC Storage vs. Local Time Filtering
&lt;/h3&gt;

&lt;p&gt;When dates are stored in UTC, they represent a global standard that eliminates the ambiguity caused by timezones. However, users typically think in terms of their &lt;strong&gt;local timezone&lt;/strong&gt;. This discrepancy becomes evident when users try to filter records by date using local time inputs.&lt;/p&gt;

&lt;p&gt;Let’s look at an example where a user’s local time inputs could lead to missed records if not handled properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example Scenario: A User in the GMT-7 Timezone
&lt;/h3&gt;

&lt;p&gt;Imagine a user in the &lt;strong&gt;GMT-7 timezone (Pacific Daylight Time)&lt;/strong&gt;. On &lt;strong&gt;September 5th, 2024&lt;/strong&gt;, they create a record at &lt;strong&gt;10:00 PM&lt;/strong&gt; in their local time. Here’s what happens behind the scenes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;September 5th, 2024, 10:00 PM GMT-7&lt;/strong&gt; is converted to &lt;strong&gt;September 6th, 2024, 05:00 AM UTC&lt;/strong&gt; and stored in the database as such.&lt;/li&gt;
&lt;li&gt;The user, however, perceives that they created this record on &lt;strong&gt;September 5th&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Filter Mismatch
&lt;/h3&gt;

&lt;p&gt;Now, suppose the user wants to query all records created on &lt;strong&gt;September 5th&lt;/strong&gt;. They input the date &lt;strong&gt;September 5th, 2024&lt;/strong&gt;, expecting to retrieve their record. However, if the system compares the input date directly to the stored &lt;strong&gt;UTC&lt;/strong&gt; date without adjusting for timezone differences, the user will &lt;strong&gt;miss&lt;/strong&gt; the record. Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The record was saved in the database as &lt;strong&gt;September 6th&lt;/strong&gt; (UTC).&lt;/li&gt;
&lt;li&gt;The user filters for &lt;strong&gt;September 5th&lt;/strong&gt; (their local time), but the system compares this against UTC, resulting in no match.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  JavaScript &lt;code&gt;Date&lt;/code&gt; Object: Inconsistencies Across Environments
&lt;/h3&gt;

&lt;p&gt;The following example code demonstrates a common problem when using the native JavaScript &lt;code&gt;Date&lt;/code&gt; object for handling date and time conversions, particularly across different environments such as Node.js and the browser (e.g., Chrome console).&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Code:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;convertToUtcStartOfDay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isoString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Step 1: Parse the ISO string into a Date object&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;localDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isoString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Step 2: Set the time to the start of the day (00:00:00) in local time zone&lt;/span&gt;
  &lt;span class="nx"&gt;localDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Step 3: Get the UTC time using toISOString() – it converts local time to UTC&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;utcStartOfDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;utcStartOfDay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This will be in UTC&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;frontendDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2023-08-22T00:00:00+05:30&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ISO string with timezone offset&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;startOfDayUtc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;convertToUtcStartOfDay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;frontendDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;startOfDayUtc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Expected output: "2023-08-21T18:30:00.000Z"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the user inputs the date &lt;code&gt;"2023-08-22T00:00:00+05:30"&lt;/code&gt; (from a GMT+5:30 timezone). The &lt;code&gt;Date&lt;/code&gt; object should convert it to the start of the day in UTC, but when executed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In Node.js&lt;/strong&gt;, the output is &lt;code&gt;2023-08-21T00:00:00.000Z&lt;/code&gt; - &lt;strong&gt;Wrong&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In Chrome's console&lt;/strong&gt;, the output is &lt;code&gt;2023-08-21T18:30:00.000Z&lt;/code&gt; - &lt;strong&gt;Correct&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This discrepancy can cause unpredictable results depending on where the code is executed. This behavior makes the &lt;code&gt;Date&lt;/code&gt; object unreliable for consistent date handling across different environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Luxon for Accurate Date Handling
&lt;/h3&gt;

&lt;p&gt;To solve this problem, it's important to use a library like &lt;strong&gt;Luxon&lt;/strong&gt; that provides consistent behavior across environments. Luxon helps you convert the user’s local input to the proper &lt;strong&gt;start&lt;/strong&gt; and &lt;strong&gt;end&lt;/strong&gt; of the day in their timezone, and then convert those times to UTC for accurate database queries.&lt;/p&gt;

&lt;p&gt;Here’s an example using &lt;strong&gt;Luxon&lt;/strong&gt; to handle this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DateTime&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;luxon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Example user input date in ISO string with timezone information from the frontend&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userInputDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2023-08-22T00:00:00+05:30&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ISO string sent by frontend&lt;/span&gt;

&lt;span class="c1"&gt;// Step 1: Parse the ISO string to get the user's local time&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userLocalDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromISO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userInputDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Step 2: Convert this date to start of the day and end of the day in the user's local timezone&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startOfDayLocal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userLocalDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;day&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// start of the day in the user's timezone&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;endOfDayLocal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userLocalDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;day&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// end of the day in the user's timezone&lt;/span&gt;

&lt;span class="c1"&gt;// Step 3: Convert these local start and end times to UTC&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startOfDayUtc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;startOfDayLocal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUTC&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toJSDate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// start of the day in UTC&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;endOfDayUtc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;endOfDayLocal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUTC&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toJSDate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// end of the day in UTC&lt;/span&gt;

&lt;span class="c1"&gt;// Step 4: Query the database using the UTC range&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;startOfDayUtc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;$lte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;endOfDayUtc&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Luxon is Better than JavaScript &lt;code&gt;Date&lt;/code&gt; Objects
&lt;/h3&gt;

&lt;p&gt;Handling date and timezone conversions directly with the native &lt;strong&gt;JavaScript &lt;code&gt;Date&lt;/code&gt; object&lt;/strong&gt; can lead to inconsistencies like the one demonstrated above. Here are a few reasons why &lt;strong&gt;Luxon&lt;/strong&gt; is a better alternative:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistency Across Environments&lt;/strong&gt;: Luxon provides consistent behavior whether the code is running in Node.js or the browser (e.g., Chrome console). This eliminates the discrepancies that arise from using the &lt;code&gt;Date&lt;/code&gt; object in different environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-in Timezone Support&lt;/strong&gt;: Luxon makes it easy to convert between timezones, while the &lt;code&gt;Date&lt;/code&gt; object doesn’t offer robust support for timezone manipulation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple Date Manipulation&lt;/strong&gt;: Setting the start or end of a day in the user's local timezone and converting it to UTC is a common task in global applications. Luxon simplifies this process with its intuitive API, while &lt;code&gt;Date&lt;/code&gt; requires complex manual handling.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Handling date and timezone conversions properly is crucial for building reliable, user-friendly applications. If developers fail to account for timezone differences when filtering records, users may miss important data—leading to confusion and potentially critical errors.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;Luxon&lt;/strong&gt; instead of the native &lt;strong&gt;JavaScript &lt;code&gt;Date&lt;/code&gt; object&lt;/strong&gt; provides consistency, better timezone handling, and easier manipulation of dates. This allows developers to create a seamless experience for users across timezones, ensuring that queries work as expected, and no records are missed during filtering.&lt;/p&gt;

&lt;p&gt;In global applications, accurate and reliable date handling is key to delivering a high-quality experience to users, regardless of their timezone.&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;/p&gt;

&lt;p&gt;Have you ever encountered a similar situation where date and timezone handling caused unexpected results in your application? How did you address it? I’d love to hear about your experiences, feedback, or any questions or concerns you might have. Feel free to share them in the comments section below. If you found this article helpful, please like and share it with others who might benefit from it!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
