Numerous external packages like Moment.js are usually used by web developers for handling date-time operations. However, after the evolution of IT, JavaScript launched a new instance constructor known as Date for handling date-time operations.
Creation of instance of Date
First of all, you need to create an object of Date. Date () constructor is used to create an object of date. We enlist the various ways to instantiate a Date.
new Date (ms)
new Date (stringdate)
new Date(y,m,d,h,min,sec,ms)
Where; ms= milliseconds, y = year, m = month, d = day, h = hour, min =minutes, and sec=seconds. Here the parameter of month starts from 0 to 11.
Date comparison in JavaScript
In this article, we are presenting different ways to contrast dates in JavaScript.The simplest method to compare dates in JavaScript is by using the getTime() function.
Compare Date in JavaScript through getTime() function
If you want to compare the date with time in JavaScript then you can use the built-in function getTime(). The getTime() function converts Date into numeric value. When values of Date are converted into numeric values then you can easily compare two dates. In this example, we use logical operators to compare two dates correspondingly.
const instancedate2 = new Date("2021-04-21")
const instancedate3 = new Date("2021-01-31")
console.log(instancedate1.getTime() > instancedate2.getTime())
console.log(instancedate1.getTime() < instancedate2.getTime())
console.log(instancedate1.getTime() === instancedate3.getTime())
console.log(instancedate1.getTime() !== instancedate3.getTime())
Output
Compare Date in JavaScript with valueOf
There is another way to compare the Date in JavaScript by using the valueOf method. It will return milliseconds for a particular time span. The valueOf() function returns either true or false while comparing two dates.
Example
const instancedate2 = new Date("2021-04-21")
const instancedate3 = new Date("2021-01-31")
console.log(instancedate1.valueOf() > instancedate2.valueOf())
console.log(instancedate1.valueOf() < instancedate2.valueOf())
console.log(instancedate1.valueOf() === instancedate3.valueOf())
console.log(instancedate1.valueOf() !== instancedate3.valueOf())
Output
Compare Date in JavaScript using getMonth(), getDate(), and getFullYear() functions
JavaScript does not have the feature to compare Date without time directly. If you want to do this sort of comparison then you need to fetch month, year, and date from the objects individually. An example is enlisted below to contrast two dates by using built-in functions of JavaScript.
return instancedate1.getFullYear() === instancedate2.getFullYear() &&
instancedate1.getMonth() === instancedate2.getMonth() &&
instancedate1.getDate() === instancedate2.getDate()
}
const instancedate1 = new Date("2020-02-31T09:11:12Z")
const instancedate2 = new Date("2020-04-21T12:10:34Z")
const instancedate3 = new Date("2020-02-31T03:34:23Z")
console.log(function_DateEqual(instancedate1, instancedate3))
console.log(function_DateEqual(instancedate1, instancedate2))
Output
Compare Date in JavaScript using toDateString() function
There is another method to compare Date in JavaScript without using time. You can use the toDateString() function for the comparison of dates by fetching only the date.
instancedate2 = new Date(instancedate2.toDateString())
instancedate3 = new Date(instancedate3.toDateString())
console.log(instancedate1.getTime() > instancedate2.getTime())
console.log(instancedate1.getTime() < instancedate2.getTime())
console.log(instancedate1.getTime() === instancedate3.getTime())
Output
Compare Date in JavaScript by through date-fns
We should not rely only on JavaScript’s date object for the date comparison. Instead of using multiple methods for basic date comparison, it is better to use fewer lines of code to develop an efficient comparison of Dates in JavaScript without considering time.
Date-fns is a third-party library for manipulation of date and its example is explained below by using different methods to confirm if the two dates are greater, lesser, or equal to each other.
let instancedate2 = new Date("2020-03-21T11:10:34Z")
let instancedate3 = new Date("2020-02-30T09:11:12Z")
//is instance date1 is written after the instance date2
console.log(datefns.isAfter(instancedate2, instancedate1))
//is instance date1 is written before the instance date2
console.log(datefns.isAfter(instancedate3, instancedate1))
//is instance date1 is equal to instance date2
console.log(datefns.isEqual(instancedate1, instancedate3)) console.log(datefns.isEqual(instancedate1, instancedate2))
Output
Conclusion
We learned everything about how to compare Date in JavaScript along with an example in this article. We also explained several ways to compare Date in JavaScript. In the first method, we compare dates through the function of getTime() then we explore a method to compare dates through the valueOf() function. We also explored several built-in functions such as getMonth(), getDate(), and getFullYear() for the comparison of Date in JavaScript. Lastly, we also discussed another function toDateString() in order to compare dates in JavaScript. All these methods have been briefly explained along with examples of how Date functions can be used in JavaScript to compare the current date with the previous and future dates.