I'm still learning JavaScript, and the only way that I've found which works for me to compare two dates without the time is to use the setHours method of the Date object and set the hours, minutes, seconds and milliseconds to zero. Then compare the two dates.

For example,

date1 = new Date()
date2 = new Date(2011,8,20)

date2 will be set with hours, minutes, seconds and milliseconds to zero, but date1 will have them set to the time that date1 was created. To get rid of the hours, minutes, seconds and milliseconds on date1 do the following:

date1.setHours(0,0,0,0)

Now you can compare the two dates as DATES only without worrying about time elements.

Answer from nexar on Stack Overflow
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › compare-without-time
How to Compare Dates Without Time in JavaScript - Mastering JS
If you want to compare two dates in JavaScript without using the time aspect, you should use the toDateString() method. It returns the date portion of the Date object as a string.
Top answer
1 of 16
958

I'm still learning JavaScript, and the only way that I've found which works for me to compare two dates without the time is to use the setHours method of the Date object and set the hours, minutes, seconds and milliseconds to zero. Then compare the two dates.

For example,

date1 = new Date()
date2 = new Date(2011,8,20)

date2 will be set with hours, minutes, seconds and milliseconds to zero, but date1 will have them set to the time that date1 was created. To get rid of the hours, minutes, seconds and milliseconds on date1 do the following:

date1.setHours(0,0,0,0)

Now you can compare the two dates as DATES only without worrying about time elements.

2 of 16
260

BEWARE THE TIMEZONE

Using the date object to represent just-a-date straight away gets you into a huge excess precision problem. You need to manage time and timezone to keep them out, and they can sneak back in at any step. The accepted answer to this question falls into the trap.

A javascript date has no notion of timezone. It's a moment in time (ticks since the epoch) with handy (static) functions for translating to and from strings, using by default the "local" timezone of the device, or, if specified, UTC or another timezone. To represent just-a-date with a date object, you want your dates to represent UTC midnight at the start of the date in question. This is a common and necessary convention that lets you work with dates regardless of the season or timezone of their creation. So you need to be very vigilant to manage the notion of timezone, both when you create your midnight UTC Date object, and when you serialize it.

Lots of folks are confused by the default behaviour of the console. If you spray a date to the console, the output you see will include your timezone. This is just because the console calls toString() on your date, and toString() gives you a local represenation. The underlying date has no timezone! (So long as the time matches the timezone offset, you still have a midnight UTC date object)

Deserializing (or creating midnight UTC Date objects)

This is the rounding step, with the trick that there are two "right" answers. Most of the time, you will want your date to reflect the local timezone of the user. What's the date here where I am.. Users in NZ and US can click at the same time and usually get different dates. In that case, do this...

// create a date (utc midnight) reflecting the value of myDate and the environment's timezone offset.
new Date(Date.UTC(myDate.getFullYear(),myDate.getMonth(), myDate.getDate()));

Sometimes, international comparability trumps local accuracy. In that case, do this...

// the date in London of a moment in time. Device timezone is ignored.
new Date(Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate()));

Deserialize a date

Often dates on the wire will be in the format YYYY-MM-DD. To deserialize them, do this...

var midnightUTCDate = new Date( dateString + 'T00:00:00Z');

Serializing

Having taken care to manage timezone when you create, you now need to be sure to keep timezone out when you convert back to a string representation. So you can safely use...

  • toISOString()
  • getUTCxxx()
  • getTime() //returns a number with no time or timezone.
  • .toLocaleDateString("fr",{timeZone:"UTC"}) // whatever locale you want, but ALWAYS UTC.

And totally avoid everything else, especially...

  • getYear(),getMonth(),getDate()

So to answer your question, 7 years too late...

<input type="date" onchange="isInPast(event)">
<script>
var isInPast = function(event){
  var userEntered = new Date(event.target.valueAsNumber); // valueAsNumber has no time or timezone!
  var now = new Date();
  var today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() ));
  if(userEntered.getTime() < today.getTime())
    alert("date is past");
  else if(userEntered.getTime() == today.getTime())
    alert("date is today");
  else
    alert("date is future");

}
</script>

See it running...

Update 2022... free stuff with tests ...

The code below is now an npm package, Epoq. The code is on github. You're welcome :-)

Update 2019... free stuff...

Given the popularity of this answer, I've put it all in code. The following function returns a wrapped date object, and only exposes those functions that are safe to use with just-a-date.

Call it with a Date object and it will resolve to JustADate reflecting the timezone of the user. Call it with a string: if the string is an ISO 8601 with timezone specified, we'll just round off the time part. If timezone is not specified, we'll convert it to a date reflecting the local timezone, just as for date objects.

function JustADate(initDate){
  var utcMidnightDateObj = null
  // if no date supplied, use Now.
  if(!initDate)
    initDate = new Date();

  // if initDate specifies a timezone offset, or is already UTC, just keep the date part, reflecting the date _in that timezone_
  if(typeof initDate === "string" && initDate.match(/(-\d\d|(\+|-)\d{2}:\d{2}|Z)$/gm)){  
     utcMidnightDateObj = new Date( initDate.substring(0,10) + 'T00:00:00Z');
  } else {
    // if init date is not already a date object, feed it to the date constructor.
    if(!(initDate instanceof Date))
      initDate = new Date(initDate);
      // Vital Step! Strip time part. Create UTC midnight dateObj according to local timezone.
      utcMidnightDateObj = new Date(Date.UTC(initDate.getFullYear(),initDate.getMonth(), initDate.getDate()));
  }

  return {
    toISOString:()=>utcMidnightDateObj.toISOString(),
    getUTCDate:()=>utcMidnightDateObj.getUTCDate(),
    getUTCDay:()=>utcMidnightDateObj.getUTCDay(),
    getUTCFullYear:()=>utcMidnightDateObj.getUTCFullYear(),
    getUTCMonth:()=>utcMidnightDateObj.getUTCMonth(),
    setUTCDate:(arg)=>utcMidnightDateObj.setUTCDate(arg),
    setUTCFullYear:(arg)=>utcMidnightDateObj.setUTCFullYear(arg),
    setUTCMonth:(arg)=>utcMidnightDateObj.setUTCMonth(arg),
    addDays:(days)=>{
      utcMidnightDateObj.setUTCDate(utcMidnightDateObj.getUTCDate + days)
    },
    toString:()=>utcMidnightDateObj.toString(),
    toLocaleDateString:(locale,options)=>{
      options = options || {};
      options.timeZone = "UTC";
      locale = locale || "en-EN";
      return utcMidnightDateObj.toLocaleDateString(locale,options)
    }
  }
}


// if initDate already has a timezone, we'll just use the date part directly
console.log(JustADate('1963-11-22T12:30:00-06:00').toLocaleDateString())
// Test case from @prototype's comment
console.log("@prototype's issue fixed... " + JustADate('1963-11-22').toLocaleDateString())

Discussions

How to compare date with another in typescript?
I think you can use the getTime method of your Date object instances to compare them. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime Minimal example: const incoming = new Date(/* date you are receiving */) const deadline = new Date() deadline.setHours(deadline.getHours() + 2) if (incoming.getTime() > deadline.getTime()) { // Error... } // Everything is fine, do your work More on reddit.com
🌐 r/node
16
7
July 1, 2022
[AskJS] Get difference between datetime without timezone reference
Not possible if you only have the 2 datetimes like that. Maybe you have more info on the api data that you receive which you can use to figure out the correct timezone of each datetime More on reddit.com
🌐 r/javascript
15
13
November 12, 2023
[JS] How can I compare Date objects ISO format properly without time?
I'm trying to add new dates within range if they don't already exist in a database (mongoDB). When the user navigates to the website, it will create… More on reddit.com
🌐 r/learnprogramming
1
2
August 25, 2022
What's are best practices when dealing with dates, without time-of-day?
4 is the usual standard. More on reddit.com
🌐 r/AskProgramming
6
1
May 19, 2022
🌐
DEV Community
dev.to › onlinemsr › javascript-compare-dates-from-chaos-to-clarity-1g23
JavaScript Compare Dates: From Chaos to Clarity - DEV Community
May 6, 2024 - But how can you compare dates without times using numbers? You can do it by rounding down the milliseconds to the closest day. You can use the JavaScript Math.floor() function for that.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-compare-date-part-only-without-comparing-time-in-javascript
How to compare date part only without comparing time in JavaScript? - GeeksforGeeks
August 29, 2024 - Comparing the date part only without comparing time in JavaScript means checking if two dates fall on the same calendar day, ignoring the specific time (hours, minutes, seconds).
🌐
Sentry
sentry.io › sentry answers › javascript › how to compare two dates with javascript
How to compare two dates with JavaScript | Sentry
January 30, 2023 - Equality operators don’t work when comparing two Date objects, because JavaScript objects are compared by reference, not value. Two Date variables are not considered to be the same unless they point to the same Date object in memory. When comparing Date objects, it’s best to first convert the Date to a timestamp number or a date string to avoid any issues with equality operators. If the Date constructor is called without the new operator, it ignores all arguments and returns a string representing the current time.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › compare-dates-without-time
Compare Two Dates, Ignoring Time, in JavaScript - Mastering JS
To compare two dates, ignoring differences in hours, minutes, or seconds, you can use the toDateString() function and compare the strings: const d1 = new Date('2020-06-01T12:00:00.000Z'); const d2 = new Date('2020-06-01T12:30:00.000Z'); const ...
🌐
sebhastian
sebhastian.com › javascript-compare-dates-without-time
Comparing JavaScript dates without time | sebhastian
December 12, 2022 - To compare dates without taking into account the time associated with each date, you need to set the time part of the date to midnight. You can do this in JavaScript by using the setHours() method provided by the Date object.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › javascript-date-comparison-how-to-compare-dates-in-js
JavaScript Date Comparison – How to Compare Dates in JS
November 7, 2024 - In this article, you have learned how to do date comparisons in JavaScript using the date Object without having to install any library.
🌐
TutorialsPoint
tutorialspoint.com › how-to-compare-only-date-part-without-comparing-time-in-javascript
How to compare only date part without comparing time in JavaScript?
August 23, 2022 - The toDateString() method can be used to return the only date part of a JavaScript date. It returns the date part as a string. We can't compare the string as date so we need to convert this string back to date. Now it sets the date with the time part to zero.
🌐
Metring
ricardometring.com › compare-date-and-time-in-javascript
How to compare Date and Time in JavaScript - Step by Step
See Difference Between Dates in JavaScript. For complete date comparison or dates without time, the logic is the same, just make sure that the Date object is initialized in the correct way.
🌐
Upmostly
upmostly.com › home › angular › compare dates angular
Comparing Dates In Javascript Without The Time Component - Upmostly
November 1, 2022 - And it occurred to me that I wasn’t just comparing dates, I was comparing the times on those date objects too! Such is Javascript, there isn’t actually a way to ask a date object for *only* the date portion for comparisons sake, it always has a time component attached.
🌐
InfluxData
influxdata.com › home › comparing two dates in javascript: a guide
Comparing Two Dates in JavaScript: A Guide | InfluxData
June 26, 2024 - Everything revolves around Date, which is a built-in JavaScript object. Despite its name, the Date object stores both date and time information. Let’s see a quick example. ... If I run the code above using Node.js, it displays a result like this: 2024-02-24T14:39:56.879Z. When we instantiate a new Date object without ...
🌐
Reddit
reddit.com › r/learnjavascript › how to compare dates without having to deal with timezones/times?
r/learnjavascript on Reddit: How to compare dates without having to deal with timezones/times?
December 27, 2021 - Questions and posts about frontend ... to JavaScript on the backend. ... РусскийFrançais简体中文한국어TürkçePortuguês (Portugal)Español (España)MagyarPolskiItalianoEspañol (Latinoamérica) So I have some data with dates in ISO 8601 format, and then I want to compare it to another date. const start = new Date('2-20-22'); // create a date based on a day of the month, time doesn't matter ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › compare-two-dates-using-javascript
JS Date Comparison - How to Compare Dates in JavaScript? - GeeksforGeeks
July 11, 2025 - Date comparisons can become complex when time zones are involved. By default, JavaScript dates are in local time unless specified otherwise.
🌐
Geshan
geshan.com.np › blog › 2024 › 11 › javascript-compare-dates
A Beginner's Guide to Comparing Dates in JavaScript
November 1, 2024 - You can directly compare JavaScript Date objects using comparison operators like <, >, >=, <=, and ===. This is the easiest and most straightforward method to compare dates. However, using these operators can lead to unpredictable results in ...
🌐
Quora
quora.com › How-do-you-compare-DateTime-in-Javascript
How to compare DateTime in Javascript - Quora
Answer (1 of 8): Simple comparison such as “is this date before another” is as simple as: [code]if (date1
🌐
UpStack
upstackhq.com › upstack blog › software development
How To Compare DateTime In JavaScript | Upstack
These apps need to display the ... and end time. In addition, you may need to use JavaScript to run reports on a particular day. A JavaScript date is an embedded object that internally stores the number of milliseconds from January 1, 1970, and externally, it is presented as a highly formatted, environmentally dependent wire. In this article, I will teach you how to compare dates and ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-compare-only-date-in-momentjs
How To Compare Only Date In Moment.js? - GeeksforGeeks
July 23, 2025 - In this approach, we are using the format method to convert both date1 and date2 to strings in the 'YYYY-MM-DD' format, which represents only the date part of each moment object. By comparing these formatted date strings, we determine if the ...
🌐
Zipy
zipy.ai › blog › how-to-compare-two-dates-with-javascript
how to compare two dates with javascript
April 12, 2024 - Before we dive into comparing dates, let's quickly review the JavaScript Date object. This built-in object represents a single point in time, providing methods and properties to work with dates and times.
🌐
Reddit
reddit.com › r/node › how to compare date with another in typescript?
r/node on Reddit: How to compare date with another in typescript?
July 1, 2022 -

I would like to do an authentication if comparing one date with the other by the time, and if it is smaller, pass the if and if it is greater than the date, drop the else.

example:

const current_hour = now.getHours() + 2;

if (hour < current_hour) {

'ok'

} else {

'error'

}

My question is how to do this since the date is not an int or a string, and I would like even if the time was smaller but if it was the next day it would fall into the else.

example

actual_hour = 6 (day 7 )

limit_hour = 8(day 6)

if (actual_hour < limit_hour) {

'ok'

} else {

'error'

}

In this case I would like it to fall into "else", even though 6 is less than 8, it would still have been the day before. Thanks