如何在引导程序/角度将ISO日期格式转换为'MM / DD / YYYY'

时间:2019-05-07 20:58:26

标签: angular bootstrap-4

希望将引导程序/角度加载时的ISO日期字符串转换为'MM / DD / YYYY'

当前加载时,日期选择器的外观如下:https://imgur.com/a/GdwnGZZ

我们需要将输入日期输入为“ MM / DD / YYYY”,然后转换为ISO格式

HTML

<form class="form-inline">
  <div class="form-group">
    <div class="input-group">
      <input class="form-control"
             name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker"
             [min]="minDate"
             [max]="maxDate"
             [style.width]="inputWidth"
             class="form-control"
      >
      <div class="input-group-append">
        <button class="btn btn-outline-secondary" (click)="d.toggle()" type="button">Pick Date
          <i class="fa fa-calendar"></i>
        </button>
      </div>
    </div>
  </div>
</form>
{{model.toISOString().substring(0,10)}}

TS

import {Component, Input} from '@angular/core';
import {NgbDateAdapter, NgbDateNativeAdapter} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'datepicker',
  templateUrl: './datepicker-popup.html',
  providers: [{provide: NgbDateAdapter, useClass: NgbDateNativeAdapter}]

})
export class NgbdDatepickerPopup {
  model: Date;
  @Input() disabled = false;
  @Input() required = false;
  @Input() displayValidation = false;
  @Input() minDate = null;
  @Input() maxDate = null;
  @Input() public inputWidth = '135px';
  dateInputMask = [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/];


  @Input() private format = 'YYYY-MM-DD';
  private inFormat = 'MM/DD/YYYY' || 'YYYY-MM-DD';
}

当前,ISO格式在所有方面都可以正常工作,但我们需要用户首先输入“ MM / DD / YYYY”格式

2 个答案:

答案 0 :(得分:0)

您可以创建一个自定义管道,该管道可以转换为所需的格式

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';

@Pipe({
  name: 'dateFormat'
})
export class DateFormatPipe extends DatePipe implements PipeTransform {
  transform(value: any, args?: any): any {
    return super.transform(value, 'mm/dd/yyyy');
  }
}

您将html更改为

[ngModel]="model | dateFormat"

要转换回,请使用一种转换为ISO格式的方法

(ngModelChange)="OnChange(event)"

答案 1 :(得分:0)

完美的解决方案是更改下面的文件,我已经编辑了代码以在输入字段中获得所需的格式(DD-MM-YYYY)。

  

ngb-date-parser-formatter.js

从下面的代码中,您将在输入字段中获得 DD-MM-YYYY 日期格式。

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { padNumber, toInteger, isNumber } from '../util/util';
/**
 * Abstract type serving as a DI token for the service parsing and formatting dates for the NgbInputDatepicker
 * directive. A default implementation using the ISO 8601 format is provided, but you can provide another implementation
 * to use an alternative format.
 */
var NgbDateParserFormatter = (function () {
    function NgbDateParserFormatter() {
    }
    return NgbDateParserFormatter;
}());
export { NgbDateParserFormatter };
var NgbDateISOParserFormatter = (function (_super) {
    __extends(NgbDateISOParserFormatter, _super);
    function NgbDateISOParserFormatter() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NgbDateISOParserFormatter.prototype.parse = function (value) {
        if (value) {
            var dateParts = value.trim().split('-');
            if (dateParts.length === 1 && isNumber(dateParts[0])) {
                return { year: toInteger(dateParts[0]), month: null, day: null };
            }
            else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) {
                return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: null };
            }
            else if (dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2])) {
                return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: toInteger(dateParts[2]) };
            }
        }
        return null;
    };

这是完成日期格式的地方

    NgbDateISOParserFormatter.prototype.format = function (date) {
        return date ?
        (isNumber(date.day) ? padNumber(date.day) : '')+"-"+ (isNumber(date.month) ? padNumber(date.month) : '') +"-"+ date.year:'';
    };


    return NgbDateISOParserFormatter;
}(NgbDateParserFormatter));
export { NgbDateISOParserFormatter };
//# sourceMappingURL=ngb-date-parser-formatter.js.map