您将如何处理不同格式的日期?
How would you handle different formats of dates?
我有不同类型的日期格式,例如:
公元 663 年 8 月 27 日 - 28 日
1945 年 8 月 22 日 5 月 19 日
1945 年 5 月 4 日 – 1945 年 8 月 22 日
5/4/1945
2-7-1232
03-4-1020
1/3/1 (year 1)
09/08/0 (year 0)
注意它们都是不同的格式,不同的顺序,有的有 2 个月,有的只有 1 个月,我尝试使用 moment js with no results, I also tried to use 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>
也想办法处理CE
或AD
或BC
为了实现这一点,我想使用的理想方法是多维数组:
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
分开,它们将成为一种新格式。我认为逻辑有点在那里,但鉴于它们都是不同的格式,你如何按照上述将这些日期拆分为多维数组?
I will be updating this answer more and more while I will build new parsers. Feel free to contribute.
所以对于这些格式,我会这样做:
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)"
]
我有不同类型的日期格式,例如:
公元 663 年 8 月 27 日 - 28 日
1945 年 8 月 22 日 5 月 19 日
1945 年 5 月 4 日 – 1945 年 8 月 22 日
5/4/1945
2-7-1232
03-4-1020
1/3/1
(year 1)
09/08/0
(year 0)
注意它们都是不同的格式,不同的顺序,有的有 2 个月,有的只有 1 个月,我尝试使用 moment js with no results, I also tried to use 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>
也想办法处理CE
或AD
或BC
为了实现这一点,我想使用的理想方法是多维数组:
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
分开,它们将成为一种新格式。我认为逻辑有点在那里,但鉴于它们都是不同的格式,你如何按照上述将这些日期拆分为多维数组?
I will be updating this answer more and more while I will build new parsers. Feel free to contribute.
所以对于这些格式,我会这样做:
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)"
]