首页 文章

ngbDatepicker日期格式:YYYY-MM-DD

提问于
浏览
0

我正在使用Angular 5作为我的项目 . 所以我在表单中有日期输入字段,我使用的是NgbDatepicker,但我无法设置yyyy-mm-dd格式 . 而且当我选择日期时,它显示为一天在约会之前 . 请帮帮我 .

<form class="form-inline">
    <div class="form-group">
      <div class="input-group">
        <input class="form-control" placeholder="yyyy-mm-dd"
               name="d2" #c2="ngModel" [(ngModel)]="model2" ngbDatepicker #d2="ngbDatepicker">
        <div class="input-group-append">
          <button class="btn btn-outline-secondary" (click)="d2.toggle()" type="button">
            calender
          </button>
        </div>
      </div>
    </div>
  </form>
  <hr/>
  <pre>Model: {{ model2 | json }}</pre>

app.component.ts

import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
model2: Date;
}

输出:型号:“2018-01-31T18:30:00.000Z”,这里我选择了2018-02-01

1 回答

  • 0

    你使用的是https://ng-bootstrap.github.io/#/components/datepicker/examples吗?还是旧版本?

    如果你正在使用ng-bootstrap,我必须说你正在使用一个datepicker-adapter(ngbDatepicker,由于缺陷是由ngDateStructure提供的)问题是这个例子是错误的

    //This function it's not correct
    toModel(date: NgbDateStruct): Date {
        return date ? new Date(date.year, date.month - 1, date.day) : null;
      }
    //Change by 
    toModel(date: NgbDateStruct): Date {
        return date ? new Date(''+date.year+'-'+date.month+'-'+date.day) : null;
      }
    

    问题是新的Date()考虑了GTM区域,所以如果你住在GTM中,第一个功能会给你一天的时间

    关于dateFormat,你有一个NgbDateParserFormatter . 核实

相关问题