Last active
April 17, 2022 07:40
-
-
Save orioltf/6039800 to your computer and use it in GitHub Desktop.
#JS: create date objects with custom date formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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