function checkDate() { var first_1 = document.getElementById('first').value(); var second_2 = document.getElementById('second').value(); if (first_1 > second_2) { alert("введен неправильный промежуток времени "); } else if (first_1 == second_2) { alert("введенные даты одинаковы "); } } 
 <input id="first" type="date"> <input id="second" type="date"> 

How to check for correctness two values ​​of the input tag, the values ​​of which are dates. The use of third-party frames is not allowed.

  • Where is your html code? What format? - Daniel
  • 3
    everything is simple ... you take and compare ...... - Air
  • <html> <head> </ head> <body> <input id = "first" type = "date"> <input id = "second" type = "date"> <script type = "text / javascript> function checkDate () {var first_1 = document.getElementById ('first'). value (); var second_2 = document.getElementById ('second'). value (); if (first_1> second_2) {alert ("wrong time interval entered" );} else if (first_1 == second_2) {alert ("the entered dates are the same");}} </ script> </ body> <html> - Dima
  • 2
    @Dima learn how to ask questions and format the code. - Daniel
  • one
    @Air that's where the difficulties you take: ...value() - Igor

2 answers 2

Without wrapping in Date.parse () -> lines are compared.

 function checkDate() { var first_1 = Date.parse(document.getElementById('first').value); var second_2 = Date.parse(document.getElementById('second').value); if (first_1 > second_2) { alert("введен неправильный промежуток времени "); } else if (first_1 == second_2) { alert("введенные даты одинаковы "); } } 
  • one
    I didn't have time to add an option) - Daniel

.val (); - Used in jQuery in js used: .value;

 function checkDate() { var first_1 = document.getElementById('first').value; var second_2 = document.getElementById('second').value; var a = Date.parse(first_1); var b = Date.parse(second_2); if (isNaN(a)){ alert("не число!"); return; }else{ if (a.toLocaleString() > b.toLocaleString() || a.toLocaleString() < b.toLocaleString()) { alert("введен неправильный промежуток времени "); }else if (a.toLocaleString() == b.toLocaleString()) { alert("введенные даты одинаковы "); } } } 
 <input id="first" type="date"> <input id="second" type="date"> <input type="button" value="Сравнить" onclick="checkDate()"> 

It will probably be more correct like this:

With primary check

 function checkDate() { var first_1 = document.getElementById('first').value; var second_2 = document.getElementById('second').value; var a = Date.parse(first_1); var b = Date.parse(second_2); if (isNaN(a) || isNaN(b)){ alert("не число!"); return; }else{ if (a.toLocaleString() > b.toLocaleString() || a.toLocaleString() < b.toLocaleString()) { alert("введен неправильный промежуток времени "); }else if (a.toLocaleString() == b.toLocaleString()) { alert("введенные даты одинаковы "); } } } 
 <input id="first" type="date"> <input id="second" type="date"> <input type="button" value="Сравнить" onclick="checkDate()"> 

  • And how, interestingly, compare dates in a string representation? - Igor
  • @Igor Now let's see - Daniel
  • pardon, I take my words back - Igor
  • @Igor what do you think? * second option - Daniel