I have a date-picker on my web application that produces the following output 25-04-2018 16:50
:
It is: day-month-year hour:minute.
The idea is that I'll have two input-text, and the first date has to be before the second date. Otherwise, swap the dates.
I want to compare two dates in this format, so I try to create a Date object:
<input onchange="date()" type="text" readonly id="datetimepicker_1"/>
<input onchange="date()" type="text" readonly id="datetimepicker_2"/>
<script>
function date() {
var start = document.getElementById("datetimepicker_1");
var end = document.getElementById("datetimepicker_2");
if (start.value.trim() && end.value.trim()) {
var dateStart = new Date(start.value);
var dateEnd = new Date(end.value);
if (dateStart > dateEnd){
document.getElementById("datetimepicker_1").value = end.value;
document.getElementById("datetimepicker_2").value = start.value;
}
}
</script>
But I have the following error: Invalid Date
. How I can compare these dates?