如何更改ngx-datepicker输入格式

时间:2019-07-10 12:38:09

标签: angular

我想以yyyy-mm-dd作为输入日期,选择日期时显示相同的格式,但以2019-07-08T12:16:10.000Z格式输入。

         <input
          class="form-control"
          style="width: 48%;display: inline;margin-left: 17px;"
          bsDatepicker
          placeholder="Month And Year"
          [bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }"
          formControlName="month_and_year" >
        </div>

我得到的格式为"month_and_year": "2019-07-08T12:16:10.000Z"。 我只想要"2019-07-08"格式

2 个答案:

答案 0 :(得分:0)

您可以这样指定配置格式:

class MultiColsSortFilterProxyModel(QtGui.QSortFilterProxyModel):
    """
    Custom QSortFilterProxyModel with possibility to
    disable given columns from model filtering
    """
    def __init__(self, parent=None):
        super(MultiColsSortFilterProxyModel, self).__init__(parent)
        self.ex_cols = []

    def set_excluded_cols(self, ex_cols):
        """
        Sets list with columns indexes that will be excluded
        from model filtering
        :param ex_cols: List<int> list of columns to exclude
        """
        self.ex_cols = ex_cols

    def excluded_cols(self):
        """
        Gets columns indexes excluded from model filtering
        :return: List<int>
        """
        return self.ex_col

    def filterAcceptsRow(self, source_row, source_parent):
        """
        Reimplemented from QSortFilterProxyModel.filterAcceptsRow()
        Works almost the same, but exclude setted columns from filtering
        """
        pattern = self.filterRegExp().pattern()
        if self.filterRegExp().isEmpty():
            return True
        if self.filterKeyColumn() == -1:
            col_count = self.sourceModel().columnCount()
            for col in xrange(col_count):
                if col not in self.ex_cols:
                    index = self.sourceModel().index(source_row, col, source_parent)
                    value = unicode(self.sourceModel().data(index))
                    # if self.filterRegExp().indexIn(value) >= 0:
                    if value.__contains__(pattern):
                        return True
            return False
        index = self.sourceModel().index(source_row, self.filterKeyColumn(), source_parent)
        if not index.isValid():
            return True
        value = unicode(self.sourceModel().data(index))
        return value.__contains__(pattern)
        # return self.filterRegExp().indexIn(value) >= 0

检查文档here

更新

我想我理解您的问题。您创建了这样的反应形式:

  [bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }

您尝试像这样在表单中访问“ month_and_year”字段:

<form novalidate [formGroup]="myForm">

<input
  class="form-control"
  style="width:48%; display: inline;margin-left: 17px;"
  bsDatepicker
  placeholder="Month And Year"
  [bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }"
  formControlName="month_and_year">

</form>

您会得到:

<pre>myForm: {{ myForm.value | json }}</pre>

由于您使用日期作为一种类型,因此现在的行为是正常的。 如果您的组件中有一个变量,并使用如下日期将其初始化:

myForm: {
  "month_and_year": "2015-01-30T23:00:00.000Z"
}

然后在您的html中使用json管道将其输出,您将得到“ 2019-07-11T08:04:38.221Z”:

this.today = new Date();

类似地,如果您添加按钮来更新“ month_and_year”,如下所示:

<pre>{{ today | json }}</pre>

您仍在使用日期类型。

仅获取年,月和日。您必须从“ month_and_year”表单字段中手动提取它:

<button (click)="setFormDateValue()">Set Form Date Value</button>

  setFormDateValue() {
    this.myForm.patchValue({ month_and_year: new Date(2015, 0, 31) });
  }

实现可以是这样的:

<pre>{{ getDateInRequiredFormat(myForm.get("month_and_year").value) }}</pre>

执行此功能后,您将获得YYYY-MM-DD格式的所选日期。

Stackblitz上的演示

答案 1 :(得分:0)

如果使用输入,则可以将日期管道添加到您的值中。

 <input type="text" class="form-control" bsDatepicker formControlName="date"  
   [(bsValue)]="newVar" value="{{ newVar | date:'yyyy- MM-dd' }}"/>