I have a date object in JavaScript and I want to figure out if that date is today. What is the fastest way of doing this?
My concern was around comparing date object
If both are Date() objects, you can use this to 'format' the date in a way that it will only compare on the year/month/day: if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));
Nearly identical question: How to check if input date is equal to today's date?
The answers based on toDateString()
will work I think, but I personally would avoid them since they basically ask the wrong question.
Here is a simple implementation:
function areSameDate(d1, d2) {
return d1.getFullYear() == d2.getFullYear()
&& d1.getMonth() == d2.getMonth()
&& d1.getDate() == d2.getDate();
}
MDN has a decent overview of the JS Date object API if this isn't quite what you need.
You could use toDateString
:
var d = new Date()
var bool = (d.toDateString() === otherDate.toDateString());
I prefer to use moment lib
moment('dd/mm/yyyy').isSame(Date.now(), 'day');
var someDate = new Date("6 Dec 2011").toDateString();
var today = new Date().toDateString();
var datesAreSame = (today === someDate);