Determining Date Equality in Javascript - javascript

I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need.
Here is my code:
var valid = true;
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
alert(d1+"\n"+d2);
if(d1 > d2) {
alert("Your check out date must be after your check in date.");
valid = false;
} else if(d1 == d2) {
alert("You cannot check out on the same day you check in.");
valid = false;
}
The javascript alert after converting the dates to objects looks like this:
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
The test to determine if date 1 is greater than date 2 works. But using the == or === operators do not change valid to false.

Use the getTime() method. It will check the numeric value of the date and it will work for both the greater than/less than checks as well as the equals checks.
EDIT:
if (d1.getTime() === d2.getTime())

If you don't want to call getTime() just try this:
(a >= b && a <= b)

var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
use two simple ways to check equality
if( d1.toString() === d2.toString())
if( +d1 === +d2)

var date = Wed Oct 07 2015 19:48:08 GMT+0200 (Central European Daylight Time);
var dateOne = new Date(date);
var dateTwo = new Date();
var isEqual = dateOne.getDate() === dateTwo.getDate()
this will give you the dates equality

Related

How do I make today's date equal to a specific date?

const today = new Date();
today.setHours(0,0,0,0); // Tue May 31 2022
let dateHoliday = new Date('Tue May 31 2022');
(today == dateHoliday) ? alert('holiday') : alert('regular day');
The code above returns regular day. I want it to be true.
You can call .getTime() on both values and then do a compare on the numeric values.
const today = new Date();
today.setHours(0,0,0,0); // Tue May 31 2022
let dateHoliday = new Date('Tue May 31 2022');
(today.getTime() === dateHoliday.getTime()) ? alert('holiday') : alert('regular day');

If statement comparing two array values not completing when it should be [duplicate]

I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need.
Here is my code:
var valid = true;
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
alert(d1+"\n"+d2);
if(d1 > d2) {
alert("Your check out date must be after your check in date.");
valid = false;
} else if(d1 == d2) {
alert("You cannot check out on the same day you check in.");
valid = false;
}
The javascript alert after converting the dates to objects looks like this:
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
The test to determine if date 1 is greater than date 2 works. But using the == or === operators do not change valid to false.
Use the getTime() method. It will check the numeric value of the date and it will work for both the greater than/less than checks as well as the equals checks.
EDIT:
if (d1.getTime() === d2.getTime())
If you don't want to call getTime() just try this:
(a >= b && a <= b)
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
use two simple ways to check equality
if( d1.toString() === d2.toString())
if( +d1 === +d2)
var date = Wed Oct 07 2015 19:48:08 GMT+0200 (Central European Daylight Time);
var dateOne = new Date(date);
var dateTwo = new Date();
var isEqual = dateOne.getDate() === dateTwo.getDate()
this will give you the dates equality

How to compare the date part alone from a date time value

I have two variables namely
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
Try clearing the time using Date.setHours:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Example Code:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
The best way would be to modify the accepted answer's if statement as follows
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
In this way, you can easily check for equality as well because the return type for setHours() is integer.
Try:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}

Check if dates match in javascript

I'm creating a custom ASP.Net validator that checks if the data entered is a non working day.
The definition of "non working day" is:
The date is either a Saturday or Sunday
The date is a national holiday
I've coded the c# stuff and have created expando attributes for the error message and a string of holiday dates. Having done that, I've created a javascript function that contains this code to check for none working days. I've removed other code for brevity.
if (sender.NonWorkingDayErrorMessage && sender.Holidays) {
// Is weekend
if (date.getDay() == 6 || date.getDay() == 0) {
sender.innerHTML = sender.NonWorkingDayErrorMessage;
return;
}
// Is holiday
var holidays = sender.Holidays.split(";");
for (var i = 0; i < holidays.length; i++) {
var h = new Date(Date.parse(holidays[i]));
if (h === date) {
sender.innerHTML = sender.NonWorkingDayErrorMessage;
return;
}
}
}
The issue I have is that the code h === date is always false - here's the output I get when I add an alert and type in 26/8/2013.
Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => false
As you can see I parse the holidays but I also test the input 'date' like this further up the function like this:
// Deconstruct string and reconstruct
// as date.
var parts = args.Value.split("/");
var day = parseInt(parts[0],10);
var month = parseInt(parts[1],10) -1;
var year = parseInt(parts[2],10);
var date = new Date(year, month, day);
// Valid date format but not a valid date
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
sender.innerHTML = sender.InvalidErrorMessage;
return;
}
Anyone got ideas as to why these two dates aren't seen as matches?
Try like this
var a = new Date(2013,12,1);
var b = new Date(2013,12,1);
a.getTime() === b.getTime()

Comparing two different date formats

I have a 2 different date formats. 1) dd/mm/yyyy 2) dd-mm-yyyy
I want to compare these 2 date formats in Javascript or Actionscript.
Is it possible?
In Javascript:
x = new Date("12/12/1999")
Sun Dec 12 1999 00:00:00 GMT-0500 (Eastern Standard Time)
y = new Date("12-13-1999")
Mon Dec 13 1999 00:00:00 GMT-0500 (Eastern Standard Time)
x == y
false
x < y
true
Hope this helps!
The easy way in AS3 with your date in String format and if you are not interesting in the Date object itself :
var date1Str:String="10/01/2010";
var date2Str:String="10-01-2010";
var equal:Boolean=date2Str.split("-").join("/")==date1Str;
trace(equal);
If you are interesting into the date object so in AS3:
var date1Str:String = "10/01/2010";
var date2Str:String = "10-01-2010";
var date1Arr:Array = date1Str.split("/");
var date2Arr:Array = date2Str.split("-");
var date1:Date = new Date(date1Arr[2], date1Arr[1] - 1, date1Arr[0]);
var date2:Date = new Date(date2Arr[2], date2Arr[1] - 1, date2Arr[0]);
var equal:Boolean = date1.getTime() == date2.getTime();
trace(equal);
You could convert the date strings to Date instances and compare them, I guess.
function parseDate(ds) {
var rv = null;
ds.replace(/(\d\d?)[-/](\d\d?)[-/](\d\d\d\d)/, function(_, dd, mm, yyyy) {
rv = new Date(parseInt(yyyy, 10), parseInt(mm, 10) - 1, parseInt(dd, 10));
});
return rv;
}
// ...
if (parseDate(d1).getTime() === parseDate(d2).getTime()) {
// ...
}
If you wanted to get fancy you could add code to cope with 2-digit years.
[edit] wow #Pete here I am a grown man and somehow I managed to avoidletting the native Date object parse date strings for me all this time :-)

Categories