How would you handle different formats of dates?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 14:38:50

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 - 28 August 663 CE
22 August 1945 19 May
May 4 1945 – August 22 1945
5-10 February 1720

JS

months = new Set(["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);
 } else if (+word < 32) {
   spacetime[0].Time.days.push(+word);
 } else if (+word < 2200) {
   spacetime[0].Time.years.push(+word);
 } else if (/\w+/.test(word)) {
   spacetime[0].Time.suffixes.push(word);
}

jSon example:

        "Time": {
            "days": [
                22
            ],
            "months": [
                "August"
            ],
            "years": [
                1945
            ],
            "suffixes": [
                "10:25",
                "(UTC+1)"
            ]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!