你会如何处理不同格式的日期? [英] How would you handle different formats of dates?
问题描述
我有不同类型的日期格式,如:
I have different types of dates formatting like:
-
27 - 8月26日663 CE
27 - 28 August 663 CE
1945年8月22日19月19日
22 August 1945 19 May
1945年5月4日 - 1945年8月22日
May 4 1945 – August 22 1945
5/4/1945
2-7-1232
03-4-1020
03-4-1020
1/3/1 (第1年)
09/08/0 (第0年)
注意它们都是不同的格式,不同的顺序,有些有2个月,有些只有一个,我试过用时刻js 没有结果,我也尝试使用约会js ,但没有运气。
Note they are all different formats, different order, some have 2 months, some only one, I tried to use moment js with no results, I also tried to use date js yet, no luck.
我试图做一些拆分:
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 ",
最终我需要自动生成的是:
Eventually what I need to auto generate is:
<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
要实现这一目标我想要使用的理想方式是多维数组:
To achieve that an ideal way I'd like to use is a multidimensional array:
time.push({
Day : [],
Month : [],
Year : [],
Prefix : []
});
可能要检查天数最多2个数字
,根据 1月,2月,3月等字符串列表检查月份。
然后年份min 3个数字到最多4个数字
然后使用一些条件处理前缀。但是,
第2年或第1年
怎么样?或如果日期是 02/9/1975
怎么样?或者分离破折号
,它们将是一种新格式。我认为逻辑有点存在,但是你会如何将这些日期分成多维数组,因为它们都是不同的格式?
Probably to check max 2 numbers for days
, check months against a list of strings like January, February, March..
and then the year min 3 numbers to max 4 numbers
and then handle the prefix with some conditionals
. But yet, how about year 2 or 1
? or how about if the date is 02/9/1975
? Or with separating dash
, they'd be a new format. I think the logic is kinda there but how would you split those dates into a multidimensional array as per above given the fact that they are all different formats?
推荐答案
我将在构建新的解析器时越来越多地更新这个答案。随意贡献。
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示例:
"Time": {
"days": [
22
],
"months": [
"August"
],
"years": [
1945
],
"suffixes": [
"10:25",
"(UTC+1)"
]
这篇关于你会如何处理不同格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!