【问题标题】:How to separate my start and end times in ionic datetime by 30 minutes如何将离子日期时间中的开始时间和结束时间分开 30 分钟
【发布时间】:2020-01-23 03:16:47
【问题描述】:

我正在使用 primeng 日历来管理医疗预约的安排,我希望在为医生选择日期时,将他的开始和结束时间分为 30 分钟并显示在我的 html 中。 这里是我医生的计划数据的json渲染:[{"id":18,"planning":null,"day":"Lundi","startHour":"08:00","endHour":"17:00","isFree":true},{"id":19,"planning":null,"day":"Mardi","startHour":"09:00","endHour":"18:00","isFree":true},{"id":20,"planning":null,"day":"Mercredi","startHour":"10:00","endHour":"19:00","isFree":true},{"id":21,"planning":null,"day":"Jeudi","startHour":"11:00","endHour":"20:00","isFree":true},{"id":22,"planning":null,"day":"Vendredi","startHour":"12:00","endHour":"21:00","isFree":true},{"id":23,"planning":null,"day":"Samedi","startHour":"00:00","endHour":"00:00","isFree":false},{"id":24,"planning":null,"day":"Dimanche","startHour":"00:00","endHour":"00:00","isFree":false}]

我希望我的 html 以 30 分钟为单位显示医生的工作时间(开始和结束时间)。 这是我检索医生计划数据的 ts:

 private loadDocdate() {

    this.httpClient.get<UserResponse>('http://127.0.0.1:8000/api/medecin/details/' + this.medecinId, this.options).subscribe((data: any) => {
      this.DocPlanning = data.medecin.planning;
      console.log(JSON.stringify(this.DocPlanning.planningLines));
    });
  } 

还有我的 HTML:

<ion-content fullscreen>
  <ion-text style="font-weight: bold; text-align: center">Choisissez votre Date</ion-text>
  <p-calendar [hidden]="dateValue" [(ngModel)]="dateValue" [inline]="true" [locale]="fr" dateFormat="dd-mm-yy"
  (click)="presentAlert()">
  </p-calendar>
   <div>
    <p-calendar [hidden]="!dateValue" [(ngModel)]="value" [timeOnly]="true" hourFormat="24"
     ></p-calendar>
  </div>
</ion-content>

我想要一个如图所示的显示:

HTML 的 presentAlert() 函数

  presentAlert() {
    const currentDate = this.dateValue;
    const weekdays = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];
    this.days = weekdays[currentDate.getDay()];
    console.log(this.days);

    this.planningMedecin = this.DocPlanning.planningLines;
    this.planningMedecin.forEach(element => {
      if (element.day === this.days && element.isFree === true ) {
        this.getAlert();
        let slots = this.getTimeSlots(element.startHour, element.endHour);
        console.log(slots);

      }

      if (element.day === this.days && element.isFree !== true) {
        this.errorAlert();
      }

    });

  }
  async getAlert() {
    const alert = await this.alertCtrl.create({
      message: 'Low battery',
      subHeader: '10% of battery remaining',
      buttons: ['ok']
    });
    await alert.present();
  }

  async errorAlert() {
    const alert = await this.alertCtrl.create({
      message: 'La date que vous avez choisie est indisponible',
      subHeader: 'Nous sommes desoles',
      buttons: ['ok']
    });
    await alert.present();
  }

谢谢

【问题讨论】:

    标签: html datetime ionic-framework ionic4 datetimepicker


    【解决方案1】:

    可以使用momentjs及时计算

    参考:https://momentjs.com/

    Extract time from moment js object

    let timeObj = moment("08:00", [moment.ISO_8601, 'HH:mm']);
    let newTimeObj = moment(timeObj).add(30, 'm').toDate();
    let newTime = moment(newTimeObj).format("HH:mm");
    
    // calculate times between 2 timeslots with 30min diff
    function getTimeSlots(startTime, endTime){
        let startTimeObj = moment(startTime, [moment.ISO_8601, 'HH:mm']);
        let endTimeObj = moment(endTime, [moment.ISO_8601, 'HH:mm']);
        let appoitmentTimes = [moment(startTimeObj).format("HH:mm")]
        while(startTimeObj<endTimeObj){
            startTimeObj = moment(startTimeObj ).add(30, 'm').toDate();
            appoitmentTimes.push( moment(startTimeObj).format("HH:mm"));
        }
        return appoitmentTimes
    }
    let slots = getTimeSlots('08:00','17:00');
    console.log(slots)
    
    

    【讨论】:

    • 在我的情况下,我想将开始时间增加 30 分钟,直到到达结束时间,那么如何处理我的变量?
    • 您好,您的功能有效,我的控制台中的布局正确。但是当一个人选择显示时间段的日期并且该人可以选择时间时,我想在我的页面上显示。我更新了我的帖子并在我的 html 中添加了 presentAlert 函数。如果您有线索或想法再次感谢
    • 请在stackblitz.com/edit/ionic-v4 中添加必要的代码。最好解决这个问题。
    • 您好,对不起,我迟到了,我已经添加了必要的代码。这是链接:stackblitz.com/edit/ionic-v4?embed=1&file=src/app/…
    • 您的代码不存在,请登录然后添加您的代码并分享
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多