【问题标题】:How to disable days untill today in datepicker?如何在日期选择器中禁用直到今天的天数?
【发布时间】:2017-04-06 10:40:51
【问题描述】:

我从这里使用datepicker https://github.com/kekeh/mydatepicker/blob/master/README.md 我需要禁用直到今天的日期,所以我需要获取当前日期的月份和年份并将其传递给 myDatePickerOptions 我使用新的 Date() 函数来获取日期我在其中得到这样的结果(2017 年 4 月 6 日星期四 15:55:09 GMT+0530(印度标准时间),所以不能使用它myDatepickerOptions 如何获得日期月份和年份?我尝试了以下代码.

import { Component, OnInit,ViewEncapsulation } from '@angular/core';
import { HeaderComponent } from  '../header/header.component';
import {IMyOptions, IMyDateModel,IMyDate} from 'mydatepicker';
import { TimepickerConfig } from 'ng2-bootstrap/timepicker';
import { FormGroup,FormControl,Validators } from '@angular/forms';

@Component({
  selector: 'app-createsession',
  templateUrl: './createsession.component.html',
  styleUrls: ['./createsession.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CreatesessionComponent implements OnInit {

  eventform : FormGroup ;

  public mytime: Date = new Date();

  private myDatePickerOptions: IMyOptions = {
        //  need use the current day,month and year 

        disableUntil: {year: 2016, month: 8, day: 10},
        dateFormat: 'dd.mm.yyyy'
    };
  constructor() { }

  ngOnInit() {
    console.log(this.mytime);

    });

  }
  onDateChanged(event: IMyDateModel) {
        // event properties are: event.date, event.jsdate, event.formatted and event.epoc
    }   




onSubmit(){
  console.log(this.eventform.value);
}
}

【问题讨论】:

    标签: angular datepicker


    【解决方案1】:

    从当前日期中提取日期、年份和月份,并将其设置为 disableUntil 属性。

    public mytime: Date = new Date();
    
    currentYear: any = this.mytime.getUTCFullYear();
    currentDate: any = this.mytime.getUTCDate();
    currentMonth: any = this.mytime.getUTCMonth() + 1; //months from 1-12
    
    private myDatePickerOptions: IMyOptions = {
        //  need use the current day,month and year 
    
        disableUntil: {year: this.currentYear, month: this.currentMonth, day: this.currentDate},
        dateFormat: 'dd.mm.yyyy'
    };
    

    【讨论】:

    • 在 ngOnInit() 方法中我应该在哪里设置当前年份、当前日期和月份??
    • 你可以把它放在 mytime 的下面。请找到我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2021-03-13
    • 2023-04-05
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多