<?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>DEV Community: David Schinteie</title>
    <description>The latest articles on DEV Community by David Schinteie (@davidschinteie).</description>
    <link>https://dev.to/davidschinteie</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%2F181724%2F51d0940a-7117-4050-9184-ef61a7757042.jpg</url>
      <title>DEV Community: David Schinteie</title>
      <link>https://dev.to/davidschinteie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidschinteie"/>
    <language>en</language>
    <item>
      <title>The Friday Evening Frustration: An Ode to Git Commands</title>
      <dc:creator>David Schinteie</dc:creator>
      <pubDate>Fri, 21 Apr 2023 08:47:40 +0000</pubDate>
      <link>https://dev.to/davidschinteie/the-friday-evening-frustration-an-ode-to-git-commands-10a3</link>
      <guid>https://dev.to/davidschinteie/the-friday-evening-frustration-an-ode-to-git-commands-10a3</guid>
      <description>&lt;p&gt;Picture this: It's Friday evening, and you've been working hard all week to complete a task assigned to you by your team. You're feeling accomplished and ready to wrap up for the weekend, but there's one final step left - pushing your work to the team's Git repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--grxQ-Er6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y5v8c5y4068gn5h4gtx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--grxQ-Er6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y5v8c5y4068gn5h4gtx.gif" alt="Funny gif" width="360" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, as you try to execute the final git command, you realize you're working on the wrong branch and you're met with unexpected roadblocks that threaten to derail your weekend plans.&lt;/p&gt;

&lt;p&gt;If you've been in this situation before, you're not alone. Git is notorious for being one of those tasks that can be super annoying and frustrating, especially when you're just about to clock out for the weekend.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore some scenarios where you have to switch your commits/changes to a different branch, so you can enjoy your weekend without the dark cloud on the horizon of unresolved git conflicts 🙈&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 1: Switch your unpushed commits to a different branch &lt;code&gt;(git reset --soft)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One common scenario that can make evenings frustrating is when you realize that you've been working on the wrong branch 🤦‍♂️&lt;/p&gt;

&lt;p&gt;Perhaps you started making changes on a feature branch, only to later realize that you should have been working on a different branch, such as a bugfix branch or a release branch.&lt;/p&gt;

&lt;p&gt;In such cases, you may need to switch all your commits to the correct branch.&lt;/p&gt;

&lt;p&gt;Let's say you're currently on a feature branch called &lt;code&gt;feature/branch1&lt;/code&gt; and you've made some commits that you want to transfer to another branch called &lt;code&gt;feature/branch2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, create a backup branch to safeguard your changes in case anything goes wrong. You can do this by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout -b feature/backup-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, switch back to the original branch &lt;code&gt;feature/branch1&lt;/code&gt; using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature/branch1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, use git log to identify the commit(s) that you want to transfer to feature/branch2. Make a note of the commit hash(es). Let's say your log looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#the work added by you, that's not yet in sync with the live repo 
commit sha-3
commit sha-2
#the last commit from the live repo 
commit sha-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;git reset --soft&lt;/code&gt; followed by the commit hash of the last commit before your changes. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # for our earlier example:
git reset –-soft sha-1
#OR
git reset --soft HEAD~2 #undo last 2 commits but keep the changes unstaged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If HEAD is pointed at &lt;code&gt;sha-3&lt;/code&gt; when we start, and then perform a &lt;code&gt;git reset –soft sha-1&lt;/code&gt;, HEAD will move to that commit.&lt;/p&gt;

&lt;p&gt;All of the changes that were committed in &lt;code&gt;sha-2&lt;/code&gt; and &lt;code&gt;sha-3&lt;/code&gt; are preserved and re-added to the index as staged changes.&lt;/p&gt;

&lt;p&gt;Now, switch to the target branch &lt;code&gt;feature/branch2&lt;/code&gt; using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature/branch2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, commit the changes to feature/branch2 using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "New commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new commit on &lt;code&gt;feature/branch2&lt;/code&gt; with the changes that you transferred from &lt;code&gt;feature/branch1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note&lt;/strong&gt; that &lt;code&gt;git reset --soft&lt;/code&gt; is a powerful command that should be used with caution, as it can potentially overwrite or delete commits permanently. It's always a good practice to create a backup branch before making any changes to ensure you can easily recover from any mistakes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 2: Duplicate all my commits in a new commit on a different branch &lt;code&gt;(git cherry-pick)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This scenario is somehow similar to the previous one, except this time you already pushed your commits to the remote repo. It also applies to other scenarios as well where you simply want to duplicate some commits from one branch to another.&lt;/p&gt;

&lt;p&gt;To do this, checkout to the branch where you want to apply changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout starting-point-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have 2 options that we'll only impact the history but in essence are doing the same:&lt;/p&gt;

&lt;p&gt;Either you duplicate your commits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# it will bring all commit changes but also stage them
git cherry-pick &amp;lt;commit-hash&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Either you bring only the changes from your commits and submit your work as a new commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# it will bring all commit changes but leave them unstaged
git cherry-pick -n &amp;lt;commit-hash&amp;gt; #OR
git cherry-pick --no-commit &amp;lt;commit-hash&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So for this step, the actual flow will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout starting-point-branch
git cherry-pick -n &amp;lt;commit-1-hash&amp;gt;
git cherry-pick -n &amp;lt;commit-2-hash&amp;gt;
git cherry-pick -n &amp;lt;commit-3-hash&amp;gt;
git cherry-pick -n &amp;lt;commit-4-hash&amp;gt;
git commit -m 'new commit with changes from 4 previous commits'
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you cherry-pick commits, git attempts to apply the changes from those commits to your current branch. However, if the changes in the cherry-picked commits conflict with the changes in your current branch, you'll be faced with resolving those conflicts before you can complete the cherry-pick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Another important note:&lt;/strong&gt; &lt;code&gt;git cherry-pick&lt;/code&gt; relies on commit hashes to identify the specific commits you want to apply, and if you're not careful, you may cherry-pick unintended changes or accidentally skip important commits, leading to confusion and potential mistakes in your codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 3: Undo a git merge/commit &lt;code&gt;(git reset --hard / git revert)&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do you undo a Git merge/commit before push?
&lt;/h3&gt;

&lt;p&gt;If you need to undo a Git merge or commit before pushing your changes to a remote repository, you can use the following commands:&lt;/p&gt;

&lt;p&gt;To reset to the last undamaged commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset --hard &amp;lt;last-undamaged-commit-sha&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you only need to reset to the state of the remote branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset --hard origin/&amp;lt;name-of-branch&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will forcefully perform a "git pull" to overwrite local files, allowing you to perform a clean reset of your branch.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you undo a Git merge after pushing the changes?
&lt;/h3&gt;

&lt;p&gt;If you have already pushed the merge commit to the remote repository, you will need to create a new commit that reverts the changes. You can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git revert -m 1 &amp;lt;merge-commit-sha&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new commit that undoes the changes made by the merge commit, effectively reverting the changes back to the state before the merge 💪🤟&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 4: Update commits to remote &lt;code&gt;(git commit --amend [--no-edit] / git push --force)&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Update commits unpushed to remote
&lt;/h3&gt;

&lt;p&gt;If you haven't pushed the merge commit to the remote repository yet and you notice a mistake in the commit message or forgot to include updates to certain files, you can use the &lt;code&gt;git commit --amend&lt;/code&gt; command to update the last commit.&lt;/p&gt;

&lt;p&gt;To update the commit message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit --amend -m "new commit message"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, if you need to add changes to files that were omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit --amend --no-edit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--no-edit&lt;/code&gt; flag will retain the same commit message, allowing you to include the missing changes to the previous commit. This can be useful to keep your commit history clean and organized, especially when the changes are related to the same work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Update commits already pushed to remote
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Please note&lt;/strong&gt; that updating commits that have already been pushed to the remote repository &lt;strong&gt;should be done with caution&lt;/strong&gt;, &lt;strong&gt;as it can potentially overwrite remote commits&lt;/strong&gt; that may not be on your local machine. &lt;strong&gt;Proceed with caution&lt;/strong&gt; and &lt;strong&gt;make sure to communicate with your team&lt;/strong&gt; before making such changes.&lt;/p&gt;

&lt;p&gt;If you need to update commits that have already been pushed to the remote repository, you can use the following commands:&lt;/p&gt;

&lt;p&gt;To force-push local changes and overwrite the remote repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push origin/remote-branch --force
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can use the git revert command to undo a specific commit with a new commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git revert &amp;lt;sha of commit&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new commit that undoes the changes made by the unwanted commit. This approach will keep a record of the changes in the commit history, but the state of the code will be restored to the previous state before the unwanted commit. Again, &lt;strong&gt;exercise caution&lt;/strong&gt; when using these commands to avoid unintentional loss of remote commits. &lt;strong&gt;Communication with your team is crucial in such scenarios.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 5: Save your work without commit &lt;code&gt;(git stash)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you find yourself in a situation where you've made some changes but aren't ready to commit yet or you need to switch to another branch, you can use git stash to save your work temporarily.&lt;/p&gt;

&lt;p&gt;To save your changes in a stash, you can simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash // saves your work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new stash with a default name, such as &lt;code&gt;stash@{0}&lt;/code&gt;, and store your changes without committing them. You can then switch to another branch or perform other tasks without worrying about committing incomplete changes.&lt;/p&gt;

&lt;p&gt;When you want to resume your work, go back to the desired branch and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash pop // continue from where you left off
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you use &lt;code&gt;git stash&lt;/code&gt; frequently and want to add a label to your stash for easier identification, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash save name-your-tmp-changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows you to provide a custom name for your stash, making it easier to locate in your list of stashes.&lt;/p&gt;

&lt;p&gt;To view your list of stashes, you can use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each stash will be displayed with its name and index.&lt;/p&gt;

&lt;p&gt;To retrieve the changes from a specific stash, you can use &lt;code&gt;git stash apply&lt;/code&gt; followed by the index of the stash. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash apply &amp;lt;index-of-stash&amp;gt; // e.g., git stash apply 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can use &lt;code&gt;git stash pop&lt;/code&gt; followed by the index of the stash, which not only retrieves the changes but also removes the stash from the list of stashes.&lt;/p&gt;

&lt;p&gt;It's worth noting that &lt;code&gt;git stash pop&lt;/code&gt; and &lt;code&gt;git stash apply&lt;/code&gt; behave differently in terms of stash history. When you use &lt;code&gt;git stash apply&lt;/code&gt;, the most recently saved stash will overwrite files in the current working tree but leave the stash history unchanged. On the other hand, the &lt;code&gt;pop&lt;/code&gt; command restores files but then deletes the applied stash.&lt;/p&gt;

&lt;p&gt;In addition to the basic stash commands, there are some other useful options available:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git stash clear&lt;/code&gt;: This command empties the stash list by removing all the stashes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git stash drop &amp;lt;stash_id&amp;gt;&lt;/code&gt;: This command deletes a specific stash from the stash list.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git stash branch &amp;lt;new_branch_name stash_id&amp;gt;&lt;/code&gt;: This command creates a new branch based on the commit the stash was created from and applies the stashed changes to it, effectively creating a new branch with the changes from the stash.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using &lt;code&gt;git stash&lt;/code&gt; can be a handy way to save your work temporarily without committing incomplete changes, allowing you to switch branches or perform other tasks with ease. However, it's important to note that stashes are not meant to be a permanent solution and should be used as a temporary measure. Make sure to properly commit your changes once they are complete and thoroughly tested.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;In the end, mastering Git commands like &lt;code&gt;git cherry-pick&lt;/code&gt;, &lt;code&gt;git reset&lt;/code&gt;, &lt;code&gt;git revert&lt;/code&gt;, &lt;code&gt;git commit --amend&lt;/code&gt;, and &lt;code&gt;git stash&lt;/code&gt; can save your Friday evening from turning into a coding nightmare.&lt;/p&gt;

&lt;p&gt;These commands offer powerful ways to selectively apply, undo, modify, and temporarily store changes in your codebase.&lt;/p&gt;

&lt;p&gt;Whether you need to pick specific commits, undo commits or move changes, revert changes, amend commits, or save changes without committing, git has you covered.&lt;/p&gt;

&lt;p&gt;Just remember to &lt;strong&gt;use them wisely&lt;/strong&gt;, review and test your changes thoroughly, and &lt;strong&gt;communicate with your team to avoid potential conflicts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With these tools, you can confidently manage your code changes and enjoy your well-deserved weekend.&lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Yup conditional validations for different scenarios but the same form</title>
      <dc:creator>David Schinteie</dc:creator>
      <pubDate>Thu, 23 Mar 2023 17:18:27 +0000</pubDate>
      <link>https://dev.to/davidschinteie/yup-conditional-validations-for-different-scenarios-but-the-same-form-536n</link>
      <guid>https://dev.to/davidschinteie/yup-conditional-validations-for-different-scenarios-but-the-same-form-536n</guid>
      <description>&lt;p&gt;Creating a form with validation can be a daunting task, especially when the validation rules change depending on different scenarios.&lt;/p&gt;

&lt;p&gt;In this blog post, we will discuss how to implement conditional validation using Yup, a powerful validation library, along with React Hook Forms and Material UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditional Validation using the when() function
&lt;/h2&gt;

&lt;p&gt;Suppose we have a form with 3 fields, "Loan Option", "Loan Amount" and "Loan Down Payment". We want to validate the amount field differently based on the loan option.&lt;/p&gt;

&lt;p&gt;For example, if the user selects a car loan, we want to make sure that amount has a maximum value of 5000. On the other hand, if the user selects a mortgage loan, we want to make sure that the value is between 15 000 and 300 000.&lt;/p&gt;

&lt;p&gt;We'll start with a basic Yup schema for our form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;loan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;typeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You must select a loan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;typeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You must specify a number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;when&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mortgage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;otherwise&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="na"&gt;downPayment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;nullable&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;p&gt;Here, we use the when() function to check the value of the loan field.&lt;/p&gt;

&lt;p&gt;If the value of loan is mortgage, the amount field will be checked to have a value between 15 000 and 300 000. Otherwise, the amount field will be checked for a max value of 5000.&lt;/p&gt;

&lt;p&gt;You can play around with this demo:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/yup-validations-with-rhf-v7-mui-v5-simple-demo-hivtgh?view=preview"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Validation using the test() function
&lt;/h2&gt;

&lt;p&gt;Now if we want the spice up a bit, imagine we have a complex scenario with 3 or more use cases 😨&lt;/p&gt;

&lt;p&gt;Yup also provides a test() function that allows us to define custom validation rules for a field. This function takes two arguments: a name for the test and a validation function.&lt;/p&gt;

&lt;p&gt;For example, suppose we want to add a custom validation rule that checks if the downpayment is min 25 for the mortgage loan (1st) or if is at least 15% for a car loan (the 2nd scenario) and if the user selects a shopping loan (our 3rd scenario) we'll have the min value for 5%.&lt;/p&gt;

&lt;p&gt;We can add the following code to our schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;downPayment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yup&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;typeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You must specify a number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;downpayment-test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;downpayment must respect the selected loan limits&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;validationContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;createError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;loan&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validationContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&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;createError&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The downpayment field is required&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loan&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mortgage Loan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loan&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Car Loan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loan&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shopping Loan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&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;p&gt;I created a get wild demo 😄 that covers some scenarios like the ones mentioned:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/yup-validations-with-rhf-v7-mui-v5-vmembm?view=preview"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you imagine, sky is the limit 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post, we discussed how to implement conditional validation using Yup, React Hook Forms, and Material UI.&lt;/p&gt;

&lt;p&gt;We used the when() and test() functions from Yup to change the validation logic applied to a field based on some conditions.&lt;/p&gt;

&lt;p&gt;With this technique, we can create flexible and reusable forms that can handle different scenarios with ease.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/atosh502/conditional-validation-of-form-fields-using-yup-393j"&gt;Conditional validation of form fields using Yup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gabrielterriaga/how-to-validate-two-fields-that-depend-on-each-other-with-yup-1ccg"&gt;How to validate two fields that depend on each other with Yup&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>yup</category>
      <category>form</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Compare dates in JavaScript</title>
      <dc:creator>David Schinteie</dc:creator>
      <pubDate>Fri, 24 Feb 2023 16:14:08 +0000</pubDate>
      <link>https://dev.to/davidschinteie/compare-dates-in-javascript-4i4b</link>
      <guid>https://dev.to/davidschinteie/compare-dates-in-javascript-4i4b</guid>
      <description>&lt;p&gt;JavaScript compares two dates by considering their year, month, day, hour, minute, second, and millisecond values. &lt;/p&gt;

&lt;p&gt;However, if we wish to compare dates only based on their day and disregard their timestamps, we need to adopt a workaround that involves creating new dates with only their year, month, and day components and then comparing them.&lt;/p&gt;

&lt;p&gt;A straightforward approach to comparing such dates is by utilizing the &lt;code&gt;differenceInCalendarDays&lt;/code&gt; function from the &lt;code&gt;date-fns&lt;/code&gt; library.&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="nx"&gt;datesAreEqual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dateA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dateB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;differenceInCalendarDays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dateA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dateB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An additional option is to utilize the &lt;code&gt;isEqual&lt;/code&gt;, &lt;code&gt;parseISO&lt;/code&gt;, and &lt;code&gt;startOfDay&lt;/code&gt;functions from the &lt;code&gt;date-fns&lt;/code&gt; library.&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="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2019-05-30&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;today&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;startOfDay&lt;/span&gt;&lt;span class="p"&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="nf"&gt;isEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parseISO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;today&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;isEqual(dateLeft, dateRight)&lt;/code&gt; function will compare two dates and return true if they are equal, taking their timestamps into consideration.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;parseISO&lt;/code&gt; function will parse a string in ISO format and return a Date object.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;startOfDay&lt;/code&gt; function will return the start of a given date with its timestamp set to 0 for hours, minutes, seconds, and milliseconds.&lt;/p&gt;

&lt;p&gt;Alternatively, without relying on any external libraries, you can strip the timestamp from a date object using the &lt;code&gt;getFullYear()&lt;/code&gt;, &lt;code&gt;getMonth()&lt;/code&gt;, and &lt;code&gt;getDate()&lt;/code&gt; functions, and compare the resulting dates using the &lt;code&gt;toISOString()&lt;/code&gt; method for string comparison.&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="nx"&gt;getDateAsDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getFullYear&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMonth&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;datesAreEqual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dateA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dateB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dateA&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="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dateB&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="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;//'2021-05-09T21:00:00.000Z'&lt;/span&gt;

&lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;'&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="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that direct comparison of Date objects using strict comparison or non-strict comparison will not work, as it will always result in false. Therefore, an alternate method such as the ones mentioned earlier should be utilized to compare Date objects.&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="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//Mon May 10 2021 00:00:00 GMT+0300 (Eastern European Summer Time)&lt;/span&gt;

&lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//false&lt;/span&gt;

&lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;getDateAsDay&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-05-10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, comparing dates can be a tricky task, especially when it comes to ignoring timestamps. While there are several methods to handle this, it's important to remember that direct comparison of Date objects will not work. &lt;/p&gt;

&lt;p&gt;Instead, utilizing functions such as &lt;code&gt;differenceInCalendarDays&lt;/code&gt;, &lt;code&gt;isEqual&lt;/code&gt;, &lt;code&gt;parseISO&lt;/code&gt;, and &lt;code&gt;startOfDay&lt;/code&gt; from the &lt;code&gt;date-fns&lt;/code&gt; library or stripping the timestamp from a Date object using the &lt;code&gt;getFullYear()&lt;/code&gt;, &lt;code&gt;getMonth()&lt;/code&gt;, and &lt;code&gt;getDate()&lt;/code&gt; functions, and comparing the resulting dates using the &lt;code&gt;toISOString()&lt;/code&gt; method, can help achieve accurate date comparisons. &lt;/p&gt;

&lt;p&gt;I hope this post has been helpful in clarifying these concepts. If you have any questions or feedback, feel free to leave a comment below. &lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>cryptocurrency</category>
      <category>bitcoin</category>
      <category>web3</category>
      <category>security</category>
    </item>
  </channel>
</rss>
