Skip to content Skip to sidebar Skip to footer

How Would You Handle Different Formats Of Dates?

I have different types of dates formatting like: 27 - 28 August 663 CE 22 August 1945 19 May May 4 1945 – August 22 1945 5/4/1945 2-7-1232 03-4-1020 1/3/1 (year 1) 09/08/0 (yea

Solution 1:

I will be updating this answer more and more while I will build new parsers. Feel free to contribute.

So for these formats, I'll do:

27-28August663CE22August1945 19MayMay41945August2219455-10February1720

JS

months = newSet(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
for(var i = 0; i < dateText.length; i++) {
  d += dateText[i] + ' ';
}
var words = d.replace("–", " ").replace("-", " ").replace(",", " ").replace("/", " ").split(' ');
words = $.grep(words, function(n, i){
    return (n !== "" && n != null);
});
var array = words;
var newArray = array.filter(function(v){return v!==''});
for (const word of newArray) {
 if (months.has(word)) {
   spacetime[0].Time.months.push(word);
 } elseif (+word < 32) {
   spacetime[0].Time.days.push(+word);
 } elseif (+word < 2200) {
   spacetime[0].Time.years.push(+word);
 } elseif (/\w+/.test(word)) {
   spacetime[0].Time.suffixes.push(word);
}

jSon example:

"Time":{"days":[22],"months":["August"],"years":[1945],"suffixes":["10:25","(UTC+1)"]

Post a Comment for "How Would You Handle Different Formats Of Dates?"