[英]How can you subtract dates with different time formats in JavaScript?
[英]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>
並且想出一種處理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
處理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.