【问题标题】:ng-bootstrap how get string or Date format no objectng-bootstrap 如何获取字符串或日期格式无对象
【发布时间】:2016-10-17 09:59:31
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: angular datepicker ng-bootstrap


    【解决方案1】:

    你可以使用NgbDateParserFormatter

    import {NgbDateParserFormatter} from '@ng-bootstrap/ng-bootstrap';
    
    constructor(private parserFormatter: NgbDateParserFormatter) {}
    ...
    let date = this.parserFormatter.format(this.model);
    

    Plunker

    【讨论】:

    • 感谢您的提示。为我工作。我真的不明白为什么默认情况下(可配置或默认)格式化程序没有集成到日期选择器和文本输入字段之间的编组和解组中。
    【解决方案2】:

    您可以提取对象的特定属性并构造您的日期字符串,如下所示:

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

    【讨论】:

      【解决方案3】:

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

      date: NgbDateStruct;
      
      ngOnInit(){
      
         this.date = {
           year: 2016,
           month: 10,
           day: 17
         }
      
      }
      

      在 HTML 中打印

      <span>{{  date  }}</span>
      

      【讨论】:

        【解决方案4】:

        以下解决方案适用于角 5-6-7-8-9:

        导入:

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

        构造函数:

        私有解析器格式化程序:NgbDateParserFormatter

        获取价值:

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

        设置值:

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

        【讨论】:

          猜你喜欢
          • 2015-11-26
          • 2011-03-05
          • 1970-01-01
          • 2018-03-13
          • 1970-01-01
          • 2014-09-28
          • 1970-01-01
          • 2013-08-14
          • 1970-01-01
          相关资源
          最近更新 更多