【问题标题】:How would you handle different formats of dates?您将如何处理不同格式的日期?
【发布时间】:2017-07-31 13:54:31
【问题描述】:

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

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

  • 1945 年 8 月 22 日 5 月 19 日

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

  • 1945 年 5 月 4 日

  • 2-7-1232

  • 03-4-1020

  • 1/3/1 (year 1)

  • 09/08/0 (year 0)

请注意,它们都是不同的格式,不同的顺序,有的有 2 个月,有的只有一个,我尝试使用 moment js 没有结果,我也尝试使用 date 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 ",

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

<ul class="Days">
  <li>27</li>
  <li>28</li>
</ul>

<ul class="Months">
  <li>August</li>
</ul>

<ul class="Year">
  <li>663</li>
</ul>

而且还要想办法处理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。但是,year 2 or 1 怎么样?或者如果日期是02/9/1975,怎么样?或者分离dash,它们将是一种新格式。我认为逻辑有点存在,但是考虑到它们都是不同的格式,您如何将这些日期拆分为如上所述的多维数组?

【问题讨论】:

  • 这是一个字符串解析问题,除了可能验证最终字符串之外,日期库不太可能提供帮助,而且不是一个小问题
  • @charlietfl 是的,事实上他们没有帮助。我想拆分字符串,检查更新后的问题并在最后提出一些额外的想法。
  • 可能需要为您从数据中抓取的已知格式构建大量正则表达式(以及每个格式的解析器),并且当您遇到不匹配的新格式时存储它,以便您可以为它写一个新的正则表达式
  • @charlietfl 好的,我将向其他用户开放以提供想法或解决方案,我将为某些特定案例研究创建另一个问题
  • 您可能必须创建自己的解析引擎、AST 和一切。

标签: javascript jquery wikipedia wikipedia-api


【解决方案1】:

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

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

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)"
            ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2013-06-21
    相关资源
    最近更新 更多