Skip to content

Instantly share code, notes, and snippets.

@orioltf
Last active April 17, 2022 07:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orioltf/6039800 to your computer and use it in GitHub Desktop.
Save orioltf/6039800 to your computer and use it in GitHub Desktop.
#JS: create date objects with custom date formats
var startBookDate = $(this).find('input.startdate').val(),
endBookDate = $(this).find('input.enddate').val(),
startBookDateObj, endBookDateObj,
startSeasonDateObj, endSeasonDateObj;
// use function parseDate() own function to get a new Date object from a custom string format, cause the format delivered from the datePicker is Swiss date friendly
// Then use the JS Date function and its "parse" method to get a number (of milliseconds) to be able to compare dates.
startBookDateObj = Date.parse( parseDate(startBookDate) );
endBookDateObj = Date.parse( parseDate(endBookDate) );
startSeasonDateObj = Date.parse( parseDate(seasonStart) );
endSeasonDateObj = Date.parse( parseDate(seasonEnd) );
function parseDate(input, format) {
format = format || 'dd.mm.yyyy'; // some default format
var parts = input.match(/(\d+)/g),
i = 0,
fmt = {};
// extract date-part indexes from the format
format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });
return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}
// Custom formats can be passed like:
// parseDate('01-31-2010', 'mm-dd-yyyy');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment