asp.net - Check date and time comparison in Javascript -
i have 4 textboxes
date , each textbox
there dropdownlist
time.
now want is,
for each textbox date can same timing should not equal in dropdownlist.
for ex:-
if in each textbox date is
textbox1: 08/02/2016 textbox2: 08/02/2016 textbox3: 08/02/2016 textbox4: 08/02/2016
timing should not same, should be
ddl1 = 9.00 ddl2 = 10.00 ddl3 = 11.00 ddl4 = 12.00
what tried 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 , time ra reminder cannot same"; } } if (strprierror != "") { alert(strprierror); return false; } else { return true; } }
but not working if add different time. kindly suggest
also, see screenshot of html
update
the date format dd/mm/yyyy
the way checking time wrong. moreover, if have check, need check of them date
object.
so need way, assuming these date textboxes:
textbox1
textbox2
textbox3
textbox4
and times are:
drptime
drptime3
drptime4
drptime5
now, need create 4 date
objects, contain both date , time. 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 need compare if there 2 way, , 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, if
condition invalid value 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 1 (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() || dt3.gettime() == dt4.gettime() ) // error
we comparing dates, if 1 condition true, i.e., 1 date equal one, need user error.
make sure date value in textbox of format:
yyyy/mm/dd
if not, not work.
working snippet
label {display: block;}
dates <label>date 1: <input type="date" id="textbox1"></label> <label>date 2: <input type="date" id="textbox2"></label> <label>date 3: <input type="date" id="textbox3"></label> <label>date 4: <input type="date" id="textbox4"></label> times <label>time 1: <input type="text" id="drptime"></label> <label>time 2: <input type="text" id="drptime3"></label> <label>time 3: <input type="text" id="drptime4"></label> <label>time 4: <input type="text" id="drptime5"></label> <button onclick="calc8();">calculate</button> <script> function calc8() { 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); 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 alert("equal times detected"); else alert("good go!"); } </script>
instructions
- use same date (say 1st feb, use datepicker).
- use different time (say
1:00
,2:00
,3:00
,4:00
). - click button, should go.
Comments
Post a Comment