coder February 2016

Check date and time comparison in Javascript

I have 4 textboxes for date and with each textbox there is dropdownlist for time.

Now what I want is,

For each textbox date can be same but the timing should not be equal in all the dropdownlist.

for ex:-

if in each textbox the date is

textbox1: 08/02/2016
textbox2: 08/02/2016
textbox3: 08/02/2016
textbox4: 08/02/2016

Timing should not be same, it should be

ddl1 = 9.00
ddl2 = 10.00
ddl3 = 11.00
ddl4 = 12.00

What I tried is like below:-

 function ValidateRecord() {
    var StrPriError = "";
    if (document.getElementById('TextBox1').value == document.getElementById('TextBox2').value || document.getElementById('TextBox3').value || document.getElementById('TextBox4').value) {
        if (document.getElementById('DrpTime').value == document.getElementById('DrpTime3').value || document.getElementById('DrpTime4').value || document.getElementById('DrpTime5').value) {
            StrPriError += "Dates and Time for RA Reminder cannot be same";
        }
    }
    if (StrPriError != "") {
        alert(StrPriError);
        return false;
    }
    else {
        return true;
    }
}

but it is not working even if I add different time. kindly suggest

Also, see the screenshot of the HTML

Image1

UPDATE

The date format will be dd/MM/yyyy

Answers


Praveen Kumar February 2016

The way you are checking the time is wrong. Moreover, if you have to check, you need to check for all of them as a Date object.

So you need to do it this way, assuming these are your date textboxes:

  • TextBox1
  • TextBox2
  • TextBox3
  • TextBox4

And the times are:

  • DrpTime
  • DrpTime3
  • DrpTime4
  • DrpTime5

Now, you need to create four Date objects, which contain both the date and time. So say:

var dt1 = new Date(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value);
var dt2 = new Date(document.getElementById('TextBox2').value + " " + document.getElementById('DrpTime3').value);
var dt3 = new Date(document.getElementById('TextBox3').value + " " + document.getElementById('DrpTime4').value);
var dt4 = new Date(document.getElementById('TextBox4').value + " " + document.getElementById('DrpTime5').value);

And now you need to compare if there are any two this way, and you will get 6 comparisons:

dt1.getTime() == dt2.getTime()
dt1.getTime() == dt3.getTime()
dt1.getTime() == dt4.getTime()
dt2.getTime() == dt3.getTime()
dt2.getTime() == dt4.getTime()
dt3.getTime() == dt4.getTime()

So, the if condition for invalid value would be:

if (dt1.getTime() == dt2.getTime() || dt1.getTime() == dt3.getTime() || dt1.getTime() == dt4.getTime() || dt2.getTime() == dt3.getTime() || dt2.getTime() == dt4.getTime() || dt3.getTime() == dt4.getTime())
    // Error

The expanded one (for demo only) is:

if (
    dt1.getTime() == dt2.getTime() || dt1.getTime() == dt3.getTime() || dt1.getTime() == dt4.getTime() ||
    dt2.getTime() == dt3.getTime() || dt2.getTime() == dt4.getTime() ||
    d 

Post Status

Asked in February 2016
Viewed 3,207 times
Voted 11
Answered 1 times

Search




Leave an answer