如何使用ionic 2从客户日期/日期选择器中获取值



这里我遇到了一个小问题,我创建了日期选择框,客户可以在其中选择4个选项,如天、周、月、自定义日期

1.如果客户选择,则从&截止日期应为当前日期。

2.如果客户选择,则从日期开始应为星期日&截止日期应为周中的当前日期。

  1. 如果客户选择月份,则从日期开始应为月份开始日期&截止日期应为当月的当前日期。

  2. 如果客户选择**自定义日期,则客户应选择其起始日期和截止日期。

下面是我的代码,我必须用下面的格式写代码

选择日期的代码

<ion-col col-6>
<ion-label>Report Type</ion-label>
<ion-select [(ngModel)]="reportType">
<ion-option *ngFor="let opt of reportTypes" [value]="opt.TypeId">{{opt.Type}}</ion-option>
</ion-select>
</ion-col>
<ion-col col-6>
<ion-label>Report Type</ion-label>
<ion-select [(ngModel)]="reportType">
<ion-option *ngFor="let opt of reportTypes" [value]="opt.TypeId">{{opt.Type}}</ion-option>
</ion-select>
</ion-col>
</ion-row>
Code for getting custom dropdown on selecting custom
<ion-row *ngIf="reportType==10">
<ion-col>
<ion-item>
<ion-label>From Date</ion-label>
<ion-datetime displayFormat="MMM DD YYYY" pickerFormat="MMM DD YYYY" [(ngModel)]="reportDataFromDate"></ion-datetime>
</ion-item>
</ion-col>
<ion-col>
<ion-item>
<ion-label>To Date</ion-label>
<ion-datetime displayFormat="MMM DD YYYY" pickerFormat="MMM DD YYYY" [(ngModel)]="reportDataToDate"></ion-datetime>
</ion-item>

打字代码:

this.reportTypes=[{TypeId:1,Type:'Day'},{TypeId:7,Type:'Week'},{TypeId:30,Type:'Month'},{TypeId:10,Type:'Custom Date'}]

//////////////按报表类型计算日期

calculateFromAndToDateByReportype(reportType){
var days; // Days you want to subtract
var currdate = new Date();
if(reportType==1){
this.reportDataFromDate=currdate;
this.reportDataToDate=currdate;
}
else {
var last = new Date(currdate.getTime() - (reportType * 24 * 60 * 60 * 1000));
this.reportDataFromDate=last;
this.reportDataToDate=currdate;
}
}

到目前为止,我得到了

但无法获得本周,即从周日开始到周的当天

月份:-起始日期应为月份开始日期,截止日期应为当前月份日期

当前日期:起始日期应为选定值,截止日期应为选择值

使用此代码:

if(day){
this.reportDataFromDate=(new Date()).toISOString();
}
if(week){
let date = new Date();
let day = date.getDay(),
diff = date.getDate() - day + (day == 0 ? -6:0);
this.reportDataFromDate=(new Date(date.setDate(diff))).toISOString();
}
if(month){
let date = new Date();
this.reportDataFromDate=(new Date(date.getFullYear(), date.getMonth(), 1)).toISOString();
}

最新更新