繁体   English   中英

您将如何处理不同的日期格式?

[英]How would you handle different formats of dates?

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

  • 公元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处理prefix with some conditionals 但是, year 2 or 1呢? 还是日期是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)"
            ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM