Last active
January 21, 2018 23:54
-
-
Save GuilhermeRossato/09d92584dc4c372488d8741099bef805 to your computer and use it in GitHub Desktop.
JS - Guess what a "date" string format was and put that information in a object
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
/* | |
* parameter: a string with a possible date | |
* returns: false if it can't decode, otherwise an object like the following: | |
* {type, hour, minute, second, year, month, day} | |
* 'type' is the numeric index of the format that the string was guessed from. | |
*/ | |
function parseDate(v) { | |
/* | |
* Every one of those options belo is a valid entry for the 'v' argument | |
* If some information is missing, it usually is replaced by current date info | |
* For example, "21/01" will be filled with todays year, hour and minute. | |
* Date seconds usually are set to zero due to conventions. | |
*/ | |
/* var options = [ | |
"99:99:99 99/99/9999", | |
"99:99:99 99/99/99", | |
"99:99:99 99/99", | |
"99:99:99", | |
"99:99 99/99/9999", | |
"99:99 99/99/99", | |
"99:99 99/99", | |
"99:99", | |
"99/99", | |
"99/99/9999", | |
"9999/99/99", | |
"9999/99/99 99:99", | |
"9999/99/99 99:99:99", | |
];*/ | |
var ret; | |
var today = new Date(); | |
var strings = (v.match(/\d+/g)); | |
var n = strings.map(n=>parseInt(n, 10)); | |
var divisions = (v.match(/\D+/g)); | |
if (divisions) { | |
divisions = divisions.join(''); | |
} else { | |
if (strings[0].length === 4 && n[0] > 1700 && n[0] < 2200) { | |
return { type: 0, hour: today.getHours(), minute: today.getMinutes(), second: 0, day: today.getDate(), month: today.getMonth()+1, year: n[0] } | |
} else { | |
return false; | |
} | |
} | |
if (divisions === ":: //") { | |
if (strings[0].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 4 || strings[4].length > 2 || strings[5].length > 4) { | |
return false; | |
} else if (strings[5].length > 2) { | |
ret = { type: 1, hour: n[0], minute: n[1], second: n[2], day: n[3], month: n[4], year: n[5] } | |
} else { | |
ret = { type: 2, hour: n[0], minute: n[1], second: n[2], day: n[3], month: n[4], year: ((n[5] > 60)?1900:2000)+n[5] }; | |
} | |
} else if (divisions === ":: /") { | |
if (strings[0].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 4 || strings[4].length > 2) { | |
return false; | |
} else { | |
ret = { type: 3, hour: n[0], minute: n[1], second: n[2], day: n[3], month: n[4], year: today.getYear()+1900 } | |
} | |
} else if (divisions === "::" || divisions === ":: ") { | |
if (strings[0].length > 2 || strings[1].length > 2 || strings[2].length > 2) { | |
return false; | |
} else { | |
ret = { type: 4, hour: n[0], minute: n[1], second: n[2], day: today.getDate(), month: today.getMonth()+1, year: today.getYear()+1900 }; | |
} | |
} else if (divisions === ": //") { | |
if (strings[0].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 2 || strings[4].length > 4) { | |
return false; | |
} else if (strings[4].length > 2) { | |
ret = { type: 5, hour: n[0], minute: n[1], second: 0, day: n[2], month: n[3], year: n[4] } | |
} else { | |
ret = { type: 6, hour: n[0], minute: n[1], second: 0, day: n[2], month: n[3], year: ((n[4] > 60)?1900:2000)+n[4] }; | |
} | |
} else if (divisions === ": /") { | |
if (strings[0].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 2) { | |
return false; | |
} else { | |
ret = { type: 7, hour: n[0], minute: n[1], second: 0, day: n[2], month: n[3], year: today.getYear()+1900 }; | |
} | |
} else if (divisions === ":") { | |
if (strings[0].length > 2 || strings[1].length > 2) { | |
return false; | |
} else { | |
ret = { type: 8, hour: n[0], minute: n[1], second: 0, day: today.getDate(), month: today.getMonth()+1, year: today.getYear()+1900 }; | |
} | |
} else if (divisions === "/") { | |
if (strings[0].length > 2 || strings[1].length > 2) { | |
return false; | |
} else { | |
ret = { type: 9, hour: today.getHours(), minute: today.getMinutes(), second: 0, day: n[0], month: n[1], year: today.getYear()+1900 }; | |
} | |
} else if (divisions === "//") { | |
if (strings[0].length > 4 || strings[0].length === 3 || strings[1].length > 2 || strings[1].length > 2) { | |
return false; | |
} else if (strings[0].length === 4) { | |
ret = { type: 10, hour: today.getHours(), minute: today.getMinutes(), second: 0, day: n[2], month: n[1], year: n[0] }; | |
} else if (strings[0].length <= 2 && n[0] < 32 && n[1] < 13) { | |
if (strings[2].length === 4) { | |
ret = { type: 11, hour: today.getHours(), minute: today.getMinutes(), second: 0, day: n[0], month: n[1], year: n[2] }; | |
} else { | |
ret = { type: 12, hour: today.getHours(), minute: today.getMinutes(), second: 0, day: n[0], month: n[1], year: ((n[2] > 60)?1900:2000)+n[2] }; | |
} | |
} else { | |
return false; | |
} | |
} else if (divisions === "// :") { | |
if (strings[0].length != 4 || strings[1].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 2 || strings[4].length > 2) { | |
return false | |
} else { | |
ret = { type: 13, hour: n[3], minute: n[4], second: 0, day: n[2], month: n[1], year: n[0] }; | |
} | |
} else if (divisions === "// ::") { | |
if (strings[0].length != 4 || strings[1].length > 2 || strings[1].length > 2 || strings[2].length > 2 || strings[3].length > 2 || strings[4].length > 2 || strings[5].length > 2) { | |
return false | |
} else { | |
ret = { type: 14, hour: n[3], minute: n[4], second: n[5], day: n[2], month: n[1], year: n[0] }; | |
} | |
} else { | |
return false; | |
} | |
//console.log(ret.hour < 24 , ret.minute < 60 , ret.second < 60 , ret.day > 0 , ret.day < 32 , ret.month > 0 , ret.month < 13 , ((ret.year > 1700 && ret.year < 2200) || ret.year === 0)); | |
if (ret.hour < 24 && ret.minute < 60 && ret.second < 60 && ret.day > 0 && ret.day < 32 && ret.month > 0 && ret.month < 13 && ((ret.year > 1700 && ret.year < 2200) || ret.year === 0)) { | |
return ret; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment