ng-bootstrap如何获取字符串或日期格式没有对象

ng-bootstrap how get string or Date format no object

我在这里使用 ng-bootstrap Datepicker: plnkr example

但现在在选择日期后我得到对象

Model: {
  "year": 2016,
  "month": 10,
  "day": 13
}

但我想以这种格式获取日期 2016-10-17

您可以提取对象的特定属性并像这样构建日期字符串:

let date = this.Model.year + '-' + this.Model.month + '-' + this.Model.day

您可以使用NgbDateParserFormatter

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

constructor(private parserFormatter: NgbDateParserFormatter) {}
...
let date = this.parserFormatter.format(this.model);

Plunker

你可以像下面这样写打字稿。

date: NgbDateStruct;

ngOnInit(){

   this.date = {
     year: 2016,
     month: 10,
     day: 17
   }

}

并在 HTML

内打印
<span>{{  date  }}</span>

下面的解决方案适用于 angular 5-6-7-8-9:

Import:

从“@ng-bootstrap/ng-bootstrap”导入 {NgbDateParserFormatter};

constructor:

私有解析器格式器:NgbDateParserFormatter

Get Value:

this.empForm.value.dobField = this.parserFormatter.format(this.empForm.value.dobField);

SetValue:

this.empForm.get('dobField').setValue(this.parserFormatter.parse(this.empForm.value.dobField));