Is there a relatively straightforward way to do a date comparison, using jquery, on a UK formatted date? I’m trying to check if the number of days selected via a date input field is greater than or equal to a set number. Unfortunately I can’t change the date format and need to work with dd/mm/yy.
UPDATE: the date I am working with is already in UK format dd/mm/yy – I am trying to convert this into a format which can be compared with the current date, to find number of days elapsed.
Working solution:
function checkDaysElapsed (input) {
var datePart = input.split("/");
year = datePart[2]; // get only two digits
month = datePart[1];
day = datePart[0];
var date1 = new Date(year,month-1,day);
var currentTime = new Date();
var month2 = currentTime.getMonth();
var day2 = currentTime.getDate();
var year2 = currentTime.getFullYear();
var date2 = new Date(year2,month2,day2);
console.log(year + '/' + month + '/' + day);
console.log(year2 + '/' + month2 + '/' + day2);
delta = date2 - date1;
// delta is the difference in milliseconds
delta_in_days = Math.round(delta / 1000 / 60 / 60/ 24);
return delta_in_days;
}
After you have the date in the correct format, you will also need to compare it to the other date. To do this, you might want to cast the date string to a
Date()object.Also note, that as soon as you have created a
Dateobject, the actual format is no longer relevant.First you need to split the date into its components, using string.split(‘/’)