manage interval of date with date objects

This commit is contained in:
Baptiste Lemoine 2020-01-24 12:24:45 +01:00
parent 1005695153
commit e227344647

View File

@ -46,7 +46,8 @@ export class DatesComponent extends BaseComponent implements OnInit {
this.config.dateList.push({ this.config.dateList.push({
literal: '', literal: '',
date_object: new Date(), date_object: new Date(),
timeList: []}); timeList: []
});
let selector = '[ng-reflect-name="dateChoices_' + (this.config.dateList.length - 1) + '"]'; let selector = '[ng-reflect-name="dateChoices_' + (this.config.dateList.length - 1) + '"]';
this.cd.detectChanges(); this.cd.detectChanges();
const elem = this.document.querySelector(selector); const elem = this.document.querySelector(selector);
@ -59,7 +60,13 @@ export class DatesComponent extends BaseComponent implements OnInit {
* change time spans * change time spans
*/ */
addTime() { addTime() {
this.config.timeList.push({literal: '', timeList: []}); this.config.timeList.push(
{
literal: '',
timeList: [],
date_object: new Date()
}
);
} }
removeAllTimes() { removeAllTimes() {
@ -103,6 +110,7 @@ export class DatesComponent extends BaseComponent implements OnInit {
emptyAll() { emptyAll() {
this.config.dateList.forEach(element => { this.config.dateList.forEach(element => {
element.literal = ''; element.literal = '';
element.date_object = new Date();
element.timeList = ['', '', '']; element.timeList = ['', '', ''];
}); });
this.config.timeList.forEach(element => { this.config.timeList.forEach(element => {
@ -110,6 +118,7 @@ export class DatesComponent extends BaseComponent implements OnInit {
}); });
} }
/** /**
* add all the dates between the start and end dates in the interval section * add all the dates between the start and end dates in the interval section
*/ */
@ -118,7 +127,11 @@ export class DatesComponent extends BaseComponent implements OnInit {
const converted = []; const converted = [];
newIntervalArray.forEach(element => { newIntervalArray.forEach(element => {
converted.push({literal: element, timeList: []}); converted.push({
literal: element.literal,
date_object: element.date_object,
timeList: []
});
}); });
this.config.dateList = [...new Set(converted)]; // add only dates that are not already present with a Set of unique items this.config.dateList = [...new Set(converted)]; // add only dates that are not already present with a Set of unique items
this.showDateInterval = false; this.showDateInterval = false;
@ -142,10 +155,13 @@ export class DatesComponent extends BaseComponent implements OnInit {
d2 = new Date(d2); d2 = new Date(d2);
const dates = []; const dates = [];
while (+d1 < +d2) { while (+d1 < +d2) {
dates.push(this.formateDate(d1)); dates.push({
literal: this.formateDate(d1),
date_object: d1
});
d1.setDate(d1.getDate() + interval) d1.setDate(d1.getDate() + interval)
} }
return dates.slice(0) return dates.slice(0);
} }
/** /**