Datepicker ng-bootstrap format

I use ng-bootstrap datepicker, but when I save the form, the date is saved as.

date: {
  day: 01,
  month: 12,
  year: 16
}

I was hoping that I could save him as something more than "2016-11-23T00:39:31.768Z"

Here is my implementation:

<div class="input-group">
  <button class="input-group-btn" (click)="d.toggle()" >
    <md-icon svgSrc="assets/images/calendar-plus.svg" style="cursor: pointer;"></md-icon>
  </button>
  <input class="form-control" placeholder="dd-mm-yyyy" name="dp" formControlName="due_date" navigation="arrows" ngbDatepicker #d="ngbDatepicker">
</div>

And form.component:

constructor( private fb: FormBuilder ) {
    this.form = this.fb.group({
      due_date: [''],
    });
  }
+4
source share
2 answers

You are using ng-bootstrap, not ng2-bootstrap (different groups). In the code that is behind it is used NgbDateStruct, which is the object{ day, month, year }

When presenting, you will need to connect and change the value to something else, for example:

onSubmit(): {
    let ngbDate = this.form.controls['due_date'].value;
    let myDate = new Date(ngbDate.year, ngbDate.month-1, ngbDate.day);
    let formValues = this.form.value;
    formValues['due_date'] = myDate;
    <...>
    http.post(url, formValues);
}

https://ng-bootstrap.imtqy.com/#/components/datepicker

NgbDateStruct interface of the NgbDatepicker model and NgbInputDatepicker Directive

The properties

     

day : , 1

     

month : , ISO 8601:1 = Jan   ... 12 = Dec

     

year : , 2016

+7

@silentsod, , datepicker NgbDateStruct string. ng-bootstrap NgbDateStruct ISO 8601 (yyyy-mm-dd). , :

import {NgbDateParserFormatter} from '@ng-bootstrap/ng-bootstrap';

...

constructor(private ngbDateParserFormatter: NgbDateParserFormatter; ... ) {
    ...
}

...

onSubmit(): {
    let ngbDate = this.form.controls['due_date'].value;
    let myDate = this.ngbDateParserFormatter.format(ngbDate); // e.g. myDate = 2017-01-01
    ...
}

: https://github.com/ng-bootstrap/ng-bootstrap/blob/master/src/datepicker/ngb-date-parser-formatter.ts

+6

Source: https://habr.com/ru/post/1661772/


All Articles