4

This is my script.

$(document).on("click","input[name='to_time[]']",function(){
    var from = $("input[name='from_time[]']").val(); // "11:15 AM"
    var to = $("input[name='to_time[]']").val(); //"10:15 AM"

    if(Date.parse(from) > Date.parse(to)){
      alert("Invalid Date Range");
    }
});

From Datepicker receiving date as this format "12:54 PM" Date.parse(from) returning as Not a number. NaN How do I parse this string to Javascript object or is there any alternative way to validate End time and From time.

Hardik Patil
  • 515
  • 1
  • 4
  • 16

1 Answers1

2

You can use Moment.js library for this. See below

var from =  "11:15 AM";
var to = "10:15 AM";

if (moment(from, 'hh:mm a') > moment(to, 'hh:mm a')) {
  alert("Invalid Date Range");
} else {
  alert("Valid Date Range");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.js"></script>

Remember to add format hh:mm a while converting time string to object.

Community
  • 1
  • 1
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25