【问题标题】:How to get values from customer dates/Datepicker using ionic 2如何使用 ionic 2 从客户日期/日期选择器中获取值
【发布时间】:2018-03-18 07:34:41
【问题描述】:

在这里我遇到了一个小问题,我创建了日期选择框,客户可以在其中选择 4 个选项,例如 Day,Week,Month,Custom date

1.如果客户选择Day,开始和结束日期应该是当天。

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'}]

/////////////按reportTypes计算日期

  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;
   }

  }

到现在为止我得到了Day

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

月份:-从日期应该是月份开始日期,到日期应该是月份当前日期

当前日期:从日期应该是选定的值,到日期应该是选定的值

【问题讨论】:

    标签: html angular ionic-framework ionic2


    【解决方案1】:

    使用此代码:

     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();
    }
    

    【讨论】:

    • 对于自定义日期选择,如果我在全天、周、月提到的选择框中使用此条件,我该如何使用
    • 天=reportType==1, 月=reportType==7 等等。请将我的代码放入您的代码中并进行测试。
    • 但是到目前为止呢?并且类型字符串不可分配到日期我得到了这个错误,我给了像这个reportDataFromDate:Date这样的变量; reportDataToDate:日期;
    • 迄今为止的所有选项中都是当前日期。 Ionic 默认使用当前日期,但您可以使用 this.reportDataToDate=(new Date()).toISOString();
    • 当我选择当前日期时,我的意思是 2017 年 10 月 6 日从和到日期我也得到 2017 年 10 月 5 日的数据?
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多