Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha)

So I am building a todo app(very common, I know), and in that, there must be a feature for due dates. Due dates are a must, in the sense that they provide a deadline for when the task must be completed.

I implemented a color based system to differenti…


This content originally appeared on DEV Community and was authored by Pratyush Srivastava

So I am building a todo app(very common, I know), and in that, there must be a feature for due dates. Due dates are a must, in the sense that they provide a deadline for when the task must be completed.

I implemented a color based system to differentiate whether a task is

currently active (due date is in future): denoted by green color,
same day (due date is the same as creation): denoted by orange,
overdue (due date is over): denoted by red and an overdue indicator

I implemented it and it worked alright but I saw that the task created today also had an overdue indicator.

I thought, what's happening here, and then I realized the time factor is involved in the comparison, if you compare two date objects there always be time involved and it will not be desired in every case (like my case)

const compareDate = (date) => {
    const current = new Date()
    const due = new Date(date)
    if(current === due) return 'orange'
    if(current > due) return 'red' //here basically
    return 'green'
}

But if you use .toDateString() to compare only date will be compared and time factor is eliminated:

const compareDate = (date) => {
    const current = new Date()
    const due = new Date(date)
    if(current.toDateString() === due.toDateString()) return 'orange' 
    if(current > due) return 'red' 
    return 'green'
}

This fixed the problem for me, so if you're facing the same problem and this helps, you can follow for more dev tips just like this!


This content originally appeared on DEV Community and was authored by Pratyush Srivastava


Print Share Comment Cite Upload Translate Updates
APA

Pratyush Srivastava | Sciencx (2025-07-22T04:47:15+00:00) Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha). Retrieved from https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/

MLA
" » Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha)." Pratyush Srivastava | Sciencx - Tuesday July 22, 2025, https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/
HARVARD
Pratyush Srivastava | Sciencx Tuesday July 22, 2025 » Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha)., viewed ,<https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/>
VANCOUVER
Pratyush Srivastava | Sciencx - » Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/
CHICAGO
" » Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha)." Pratyush Srivastava | Sciencx - Accessed . https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/
IEEE
" » Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha)." Pratyush Srivastava | Sciencx [Online]. Available: https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/. [Accessed: ]
rf:citation
» Quick Tip: How I Compare Dates Properly in React (JavaScript Date Gotcha) | Pratyush Srivastava | Sciencx | https://www.scien.cx/2025/07/22/quick-tip-how-i-compare-dates-properly-in-react-javascript-date-gotcha/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.