nightingale2k1
nightingale2k1

Reputation: 10335

datepicker always show Day-1

I use angular 5 Reactive Form and ngx-bootstrap datepicker for date input.

<div class="col-sm-3 form-group">
            <input type="text" class="form-control" 
            placeholder="End Date" 
            formControlName="endDate" 
            [bsConfig]="{ containerClass: 'theme-dark-blue', dateInputFormat: 'YYYY-MM-DD' }"
            bsDatepicker> 
        </div>
ngOnInit(){
  this.createEditForm();
}

createEditForm(){
this.editForm = this.fb.group(
    {
        id: [this.id],
        startDate: [this.startDate, Validators.required],
        endDate: [this.endDate],
        annualSalary: [this.annualSalary, Validators.required],
        userId: [this.userId]
    });

}

but when I change input a date, the front end display correct one (like when I type 2018-01-03 it display 2018-01-03) but the form value display like Day-1 (2018-01-02)

I have tried to set the date to string but still doesn't work.

Date Input get wrong date

Also is there a way to convert every date to string just taking YYYY-MM-DD only without the time and zone ?

I use ngx-boostrap 2.0.5 (trying 3.0.1 it produce errors)

Upvotes: 0

Views: 1151

Answers (2)

dam
dam

Reputation: 255

I used this function onHidden it's work for me,
hope it helps you (;

CutTimeZoneDate(){
        let d:Date = this.form.get(your-nameform-control).value
        d.setHours(d.getHours() - d.getTimezoneOffset() / 60);
        this.form.get(your-nameform-control).setValue(d)

Upvotes: 1

ling yu
ling yu

Reputation: 66

<p><code>ngModel</code> property sets two-way data binding in this example</p>
<div class="row">
  <div class="col-xs-12 col-12 col-md-4 form-group">
     <input class="form-control" #drp="bsDaterangepicker" bsDaterangepicker 
         [(ngModel)]="bsRangeValue">
  </div>
<div class="col-xs-12 col-12 col-md-3 form-group">
<button class="btn btn-success" (click)="drp.toggle()" [attr.aria- 
      expanded]="drp.isOpen">Date Range Picker</button>

you forget to bind model ngx-bootstrap/datepicker

Upvotes: 0

Related Questions