How to get yesterday's date in JavaScript

Getting yesterday’s date is essential for data comparisons, log analysis, and building time-sensitive application features. As the creator of CoreUI with over 25 years of development experience, I’ve implemented date calculations in analytics dashboards and reporting components countless times. The most reliable method is using setDate() with getDate() - 1 which automatically handles month and year boundaries. This approach eliminates the complexity of manual date arithmetic and daylight saving time issues.

Use setDate() to subtract one day and get yesterday’s date reliably.

const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)

The setDate() method automatically handles date arithmetic complexities when subtracting days. When getDate() - 1 results in a value less than 1, JavaScript automatically adjusts to the previous month and decrements the year if crossing January 1st. This method correctly handles February in leap years, varying month lengths (28-31 days), and all edge cases without manual calculations.

Best Practice Note:

This is the same approach we use in CoreUI analytics components for reliable historical data comparisons. Create a new Date object first (new Date(originalDate)) to avoid mutating existing date references in your application.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author