Skip to content

Instantly share code, notes, and snippets.

@alokstha1
Created August 2, 2017 11:44
Show Gist options
  • Save alokstha1/86852f61212dcff26ca89822ec72b6e0 to your computer and use it in GitHub Desktop.
Save alokstha1/86852f61212dcff26ca89822ec72b6e0 to your computer and use it in GitHub Desktop.
Compare two dates in jquery
function myStringToDate(str) {
var arr = str.split("-"); // split string at slashes to make an array
var yyyy = arr[2] - 0; // subtraction converts a string to a number
var jsmm = arr[1] - 1; // subtract 1 because stupid JavaScript month numbering
var dd = arr[0] - 0; // subtraction converts a string to a number
return new Date(yyyy, jsmm, dd); // this gets you your date
}
var from_date = myStringToDate(jQuery('#from-date').val());
var to_date = myStringToDate(jQuery('#to-date').val());
if ( to_date.getTime() > from_date.getTime() ) {
//Do your operations
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment