Angular 4 ngx-bootstrap datepicker-如何在单击“提交”时将DATE-FORMAT从DD-MM-YYYY转换为YYYY-MM-DD

时间:2018-06-22 10:10:14

标签: angular datepicker bootstrap-datepicker ngx-bootstrap date-pipe

我在这里将ngx-bootstrap datepicker与DatePipe一起使用

  

如何在单击“提交”时将DATE-FORMAT从DD-MM-YYYY转换为YYYY-MM-DD

*我的模板-component.html

 <input type="text"
                   placeholder="FromDate"
                   class="form-control"
                   placement="top"
                   formControlName="fromDate"  [bsConfig]="{ dateInputFormat: 'DD-MM-YYYY' }"
                   bsDatepicker style="padding: 16px">

component.ts

ngOnInit() {
    this.LicenseDistributionForm.controls['toDate'].setValue(this._datePipe.transform(this.today, 'dd-MM-yyyy'));

this.LicenseDistributionForm.controls['fromDate'].setValue(this._datePipe.transform(this.firstDay, 'dd-MM-yyyy'));
    }

    submit() {
    if (!this.LicenseDistributionForm.valid) {
      this.validateAllFormFields(this.LicenseDistributionForm);
      console.log('Not valid!');
      return;
    }
    const params: any = {};

    params.fromDate = this.LicenseDistributionForm.value.fromDate;
    params.toDate = this.LicenseDistributionForm.value.toDate;


    {

    console.log('params', params);
         }
    this.organizationService.getLicenseDistributionSPDate( params.fromDate, params.toDate )
      .subscribe(licensedistributions => {
        this.licensedistributions = licensedistributions;
        console.log(licensedistributions, 'licensedistributions');
        // this.licensedistributionLoader=true;
      });

2 个答案:

答案 0 :(得分:0)

使用momentjs

moment(dateDDMMYYYY, 'DD-MM-YYYY').format('YYYY-MM-DD');

您还可以对该语句应用更多验证。

var momentObj = moment(dateDDMMYYYY, 'DD-MM-YYYY', true);

if ( !momentObj.isValid() ) throw "Invalid date";

dateYYYYMMDD = momentObj.format('YYYY-MM-DD');

答案 1 :(得分:0)

不确定您的日期管道,但是,我更喜欢momentjs。请遵循以下网址:

http://momentjs.com/docs/#/displaying/format/