你会如何处理不同格式的日期?

我有不同类型的日期格式,如:

  • 公元663年8月27日至28日

  • 1945年8月22日19日

  • 1945年5月4日 – 1945年8月22日

  • 1945年5月4日

  • 1232年2月7日

  • 1020年3月4日

  • 1/3/1 (year 1)

  • 09/08/0 (year 0)

注意它们都是不同的格式,不同的顺序,有的有2个月,有的只有一个,我试过用js没有结果,我也尝试使用日期js ,没有运气。

我试着做一些拆分:

 dates.push({ Time : [] }); function doSelect(text) { return $wikiDOM.find(".infobox th").filter(function() { return $(this).text() === text; }); } dateText = doSelect("Date").siblings('td').text().split(/\s+/g); for(var i = 0; i < dateText.length; i++) { d += dateText[i] + ' '; } dates[0].Time.push(d); 

但结果是:

 "Time": [ "27 - 28 August 663 CE ", 

最终我需要自动生成的是:

 
  • 27
  • 28
  • August
  • 663

并且还想到一种处理CEADBC

为了达到这个目的,我想要使用的是一个多维数组:

 time.push({ Day : [], Month : [], Year : [], Prefix : [] }); 

可能要检查max 2 numbers for days ,检查月份与January, February, March..等字符串列表,然后是最小3 numbers to max 4 numbers的年份,然后prefix with some conditionals处理prefix with some conditionals 。 但是,第year 2 or 1呢? 或者如果日期是02/9/1975怎么02/9/1975 ? 或者使用分离dash ,它们将是一种新格式。 我认为逻辑有点存在,但是如果将这些日期分成多维数组,它们将如何将它们分成多个数组?

我将在构建新的解析器时越来越多地更新这个答案。 随意贡献。

所以对于这些格式,我会这样做:

 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示例:

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