Skip to content

Instantly share code, notes, and snippets.

@AlexanderHentzsch
Last active November 21, 2019 14:45
Show Gist options
  • Save AlexanderHentzsch/297005ec96fe951cd27e695438dd5c26 to your computer and use it in GitHub Desktop.
Save AlexanderHentzsch/297005ec96fe951cd27e695438dd5c26 to your computer and use it in GitHub Desktop.
/** Format: DD-MM-2YYY HH:MM **/
const isDateValid = (value) => {
const isSpellingValid = (value) => {
/** Only for dates older than 2000 **/
const regExpString = /([012][0-9]|3[0-1])\.(0[1-9]|1[0-2])\.2([0-9]{3}) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])/;
let matchResult = value.match(regExpString);
return matchResult[0] === matchResult.input;
};
const isDateStringValid = (value) => {
const arr = value.split(" ");
const d = arr[0].split(".");
const validDateString = [d[2], d[1], d[0]].join("-") + " " + arr[1];
return new Date(validDateString).toString() !== "Invalid Date"
};
let result = false;
/** It is necessary to check the spelling so that no mistakes can occur when assembling the date-string. **/
if (isSpellingValid(value)) {
if (isDateStringValid(value)) {
result = true;
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment