angular2-date-picker-仅显示当前日期时间

时间:2019-06-12 08:45:56

标签: html css angular typescript datetime

我在我的angular项目中使用angular2-date-picker插件,我想显示当前时间-5分钟,所以我已经在我的component.ts文件中编写了ModifyDate()

component.ts

      modifyDate() {
      let modifiedDate=new Date();
      modifiedDate.setMinutes(modifiedDate.getMinutes()-5);
      console.log("modified: "+modifiedDate);
      return modifiedDate;
     }


     //datetime picker
     startDate: Date =this.modifyDate();
     settings = {
     bigBanner: true,
     timePicker: true,
     format: 'dd-MM-yyyy hh:mm a',
     defaultOpen: false,
     closeOnSelect:false
};

component.html 文件:

    <angular2-date-picker class="form-control" id="datetimepicker" (onDateSelect)="onDateSelect($event)"  name="datetime" [(ngModel)]="startDate" [settings]="settings"></angular2-date-picker>

构建以上代码后,在我的DOM中,获得以下几行:

enter image description here

如您所见,在标记中ng-reflect-model分配了正确的数据,但是在下面的元素中,无论如何我都获得了当前日期时间,因此,在我的应用程序中显示了当前时间。您能提出任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

无法复制您面临的问题。也许有一些东西使它无法工作...

相关的 HTML

<angular2-date-picker [(ngModel)]="date" [settings]="settings" name="angular-calendar" (onDateSelect)="onDateSelect($event)"
 ngDefaultControl></angular2-date-picker>
<br/>
    <p>
        Time from selection: <mark> {{date | date:'EE, MMM d, y, H:mm:ss zzzz' }}</mark>
    </p>
    <p>
        minus 5 minutes: <mark>{{newVar}}</mark>
    </p>

相关的 TS

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular 6';
  date = new Date();
  newVar: string = '';
  datePulled:boolean=false;
  settings = {
    bigBanner: true,
    timePicker: true,
    format: 'dd-MM-yyyy',
    defaultOpen: true
  }
  constructor() {
    /* Update #2 */
    if(!this.datePulled){
      let someVar = new Date(this.date);
      someVar.setMinutes(someVar.getMinutes() - 5);
      this.date = someVar;
      this.datePulled = true;
    }
  }
  onDateSelect(evt) {
    if(!this.datePulled){
      let someVar = new Date(this.date);
      someVar.setMinutes(someVar.getMinutes() - 5);
      /* Update #1 */
      this.date = someVar;
      this.newVar = someVar.toString();
      console.log(evt, ' & date:', this.newVar);
    this.datePulled = true;
    }
  }
}

完成working stackblitz here

更新#1 :根据请求者的评论,原始选择的日期将推迟5分钟

更新#2 :根据请求者的评论,页面加载时加载的日期也会拉回5分钟

更新#3 :根据请求者的评论,时间选择器现在设置为true;

enter image description here