【问题标题】:Angular 2 , month and year only needed in datepickerAngular 2 ,仅在 datepicker 中需要月份和年份
【发布时间】:2023-04-08 03:41:01
【问题描述】:

我需要像这样通过日期 2018-03 使用 ngx-bootstrap 的 BsDatepickerModule,

我得到了这样的结果 2018-03-04 通过使用代码

......
self.dateSales = dateofSales.toISOString().split('T')[0];
.......

帮我将日期转换为 yyyy-mm 和 yyyy 格式

【问题讨论】:

    标签: angular datepicker angular2-forms angular-date-format


    【解决方案1】:

    dateinputformat 适合你吗?试试这个:

      <input
        class="form-control"
        [minDate]="minDate"
        [maxDate]="maxDate"
        #dp="bsDatepicker"
        bsDatepicker
        formControlName="myDate"
        [bsConfig]="{ dateInputFormat: 'YYYY-MM' }"
      >
    

    或者,如果你想格式化输出,你可以使用你拥有的或者你可以使用这样的东西:

    const formattedDate = `${dateofSales.getFullYear()}-${dateOfSales.getMonth() < 9 '0' : ''}${(dateOfSales.getMonth() + 1)}`;
    

    或者更详细一点:

    const YYYY = `${date.getFullYear()}`;
    let MM = `${date.getMonth() + 1}`;
    if (MM.length < 2) MM = `0${MM}`;
    const formattedDate = `${YYYY}-${MM}`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2013-02-05
      • 1970-01-01
      • 2018-08-16
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多