Here I am using the ng bootstrap and angular 6 for the inline date picker. Here I am setting dates programmatically till now its fine but the issue is when I am setting last 7 days to date it has to be displayed from date as 02-9-2018 and to date as 17-09-2018. Here's the problem: as I am displaying the date separate calendar, the "From" date is displayed on calendar 1, and the "To" date on calendar 2.
Now what I am trying to figure out is, how can I display these only one calendar? I mean if last 7 days or last fifteen days or month are in the same month, then it doesn't have to display in another calendar. If it is not in the current month then it has to display 2 dates in different calendars.
Below is my code:
.ts code
dateSelect(event, dpp, dpt) {
var target = event.target || event.srcElement || event.currentTarget;
var idAttr = target.attributes.id;
var value = idAttr.nodeValue;
if (value == 'seven') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "10"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'fifteen') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "02"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'thirty') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "18"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'month') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "01"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
}
.html code
<ngb-datepicker #dpp [(ngModel)]="OneModel" (navigate)="dates = $event.next"></ngb-datepicker>
<ngb-datepicker #dpt [(ngModel)]="TwoModel" (navigate)="datess = $event.next"></ngb-datepicker>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="seven">Last 7 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="fifteen">Last 15 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="thirty">Last 30 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="month">This Month</button>
My working stackblitz
URL