【问题标题】:Disable dates in angular禁用角度日期
【发布时间】:2021-03-18 13:47:33
【问题描述】:

我正在使用带有开始和结束日期的角度日历。 我想禁用以前的日期,同时选择开始日期和结束日期。

<input type = "date"/>

请帮我实现这个功能。

提前致谢。

【问题讨论】:

    标签: angular date disable


    【解决方案1】:

    您可以使用input 标签的maxmin 属性

    例如:&lt;input type="date" min="2020-01-01" max="2020-03-31"&gt;

    minmax 的格式在date input 的情况下为YYYY-MM-DD

    【讨论】:

    • 感谢您的回复。但我的日期格式是 dd/mm/yyyy 先生。
    • 我将尝试分享一个有效的堆栈闪电战。请先生帮帮我。
    • 好的,请分享!
    • @ria ,您可以通过 JavaScript 轻松获取所需的日期格式。如果您想要扩展功能,可以查看moment.js library
    • 在您的代码中,您可以简单地使用 Angular 的日期管道格式化日期并将其传递给日期输入。例如:&lt;input type="date" min="{{todaysdate | date: 'yyyy-MM-dd'}}" /&gt; 您可以将日期格式更改为yyyy-MM-dd,这应该可以满足您的要求。我希望这会有所帮助,如果是这样,我将不胜感激:)
    【解决方案2】:

    希望对您有所帮助,我使用 angular/common 中的 DatePipe

    在组件中

    import { DatePipe } from "@angular/common";
    
    export class AppComponent {
       now: any;
    
       ngOnInit() {
         const datePipe = new DatePipe('en-Us');
         this.now = datePipe.transform(new Date(), 'yyyy-MM-dd');
       }
    }
    

    在html中

    <input type="date" [min]="now" required="required" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-09
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2021-05-31
      • 2016-12-06
      相关资源
      最近更新 更多