在 Javascript / Typescript 的 JSON 对象中查找所有缺失的日期
Find all missing dates in an JSON object in Javascript / Typescript
在我的应用程序中,我需要显示缺少的日期范围。我试图获得第一个和最后一个日期,但被打动了进一步进行。当我尝试时,我只得到第一个缺失日期,但预期输出是显示从开始日期到结束日期的所有缺失日期。
https://jsfiddle.net/bgef59x2/3/
https://stackblitz.com/edit/ionic-rknmsc?file=pages%2Fhome%2Fhome.ts
var data={
"dataObj"=[
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
]}
我的开始日期是“2020-04-13”,结束日期是“2020-04-21”
预期输出:
const result =["2020-04-14","2020-04-15","2020-04-16","2020-04-17","2020-04-18"]
指导我在 JavaScript/ Typescript 中实现预期结果。我没有在我的应用程序中包含 momentJs。
Web,只是从date到date只创建一个llop,检查是否在数组中a push in array
missingDates:string[]=[]
const missingDates: string[] = [];
const from = new Date(this.data[0].custom_date);
const to = new Date(this.data[this.data.length - 1].custom_date);
for (let fecha = from;fecha < to;fecha = new Date(fecha.getTime() + 24 * 60 * 60 * 1000)) {
const date =fecha.getFullYear() +"-" +
("00" + (fecha.getMonth() + 1)).slice(-2) +"-" +
("00" + fecha.getDate()).slice(-2);
if (!this.data.find(x => x.custom_date == date)) missingDates.push(date);
}
console.log(missingDates)
存在更好的解决方案,但此脚本按预期排除了日期:
var dataObj = [
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
];
var dates = dataObj.map(d => d.custom_date)
console.log(getMissingDates(dates));
function getMissingDates(dates) {
var datesParsed= dates.map(d => new Date(d)).sort()
var allExpectedDates = [];
var missingDates = [];
var from = datesParsed[0];
var to = datesParsed[datesParsed.length - 1];
var current = from;
while (current <= to) {
allExpectedDates.push(formatDate(current));
current.setDate(current.getDate() + 1);
}
return allExpectedDates.filter(el => {
return dates.indexOf(el) === -1;
});
}
// from :
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
在我的应用程序中,我需要显示缺少的日期范围。我试图获得第一个和最后一个日期,但被打动了进一步进行。当我尝试时,我只得到第一个缺失日期,但预期输出是显示从开始日期到结束日期的所有缺失日期。
https://jsfiddle.net/bgef59x2/3/
https://stackblitz.com/edit/ionic-rknmsc?file=pages%2Fhome%2Fhome.ts
var data={
"dataObj"=[
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
]}
我的开始日期是“2020-04-13”,结束日期是“2020-04-21” 预期输出:
const result =["2020-04-14","2020-04-15","2020-04-16","2020-04-17","2020-04-18"]
指导我在 JavaScript/ Typescript 中实现预期结果。我没有在我的应用程序中包含 momentJs。
Web,只是从date到date只创建一个llop,检查是否在数组中a push in array
missingDates:string[]=[]
const missingDates: string[] = [];
const from = new Date(this.data[0].custom_date);
const to = new Date(this.data[this.data.length - 1].custom_date);
for (let fecha = from;fecha < to;fecha = new Date(fecha.getTime() + 24 * 60 * 60 * 1000)) {
const date =fecha.getFullYear() +"-" +
("00" + (fecha.getMonth() + 1)).slice(-2) +"-" +
("00" + fecha.getDate()).slice(-2);
if (!this.data.find(x => x.custom_date == date)) missingDates.push(date);
}
console.log(missingDates)
存在更好的解决方案,但此脚本按预期排除了日期:
var dataObj = [
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
];
var dates = dataObj.map(d => d.custom_date)
console.log(getMissingDates(dates));
function getMissingDates(dates) {
var datesParsed= dates.map(d => new Date(d)).sort()
var allExpectedDates = [];
var missingDates = [];
var from = datesParsed[0];
var to = datesParsed[datesParsed.length - 1];
var current = from;
while (current <= to) {
allExpectedDates.push(formatDate(current));
current.setDate(current.getDate() + 1);
}
return allExpectedDates.filter(el => {
return dates.indexOf(el) === -1;
});
}
// from :
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}