Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BsDatepickerConfig dateInputFormat doesn't work when patching values manually. #2809

Closed
pedroaraujo74 opened this issue Oct 6, 2017 · 31 comments

Comments

@pedroaraujo74
Copy link

pedroaraujo74 commented Oct 6, 2017

The DateInputFormatter works perfectly and displays the desired format when selecting the date through the datepicker but when I patch the value through the form like this: this.form.patchValue({ date_begin: new Date() }) it stays with the default MM/DD/YYYY format.

The datepicker:
bsDatepicker #d="bsDatepicker" [bsConfig]="{ dateInputFormat: 'DD-MM-YYYY' }"

Does anyone know a fix for this?

edit by @valorkin: solution #2809 (comment)

@stiffold
Copy link

Same problem, pls pls fix.

@Nokhoneryu
Copy link

Same problem here! Can it be fixed or done in other ways?

@aurerua
Copy link

aurerua commented Oct 13, 2017

Same problem when initialising and binding through [(ngModel)] to a date formated as 'YYYY-MM-DD'.

@abhishekdgeek
Copy link

Same problem here. Any update for this? Or a method to patch values using some internal method?

@javilingt
Copy link

I have the same problem. When I want to init the input with any value, the format is wrong. Only works when I select the date in the calendar.

@cyberabis
Copy link

Badly need this fixed! Does someone have a workaround?

KrzysztofJaneczko pushed a commit to KrzysztofJaneczko/ngx-bootstrap-temp-fix that referenced this issue Oct 17, 2017
@dottodot
Copy link

This post suggests it not currently possible and a feature request needs submitting #2702

@cyberabis
Copy link

KrzysztofJaneczko - thanks! using this for now as a workaround.

@alexisAvenel
Copy link

To fix this issue you have to add formControlName to the input like this.

in component.ts

import * as moment from 'moment';
import {BsDatepickerConfig} from 'ngx-bootstrap/datepicker';
import {defineLocale} from 'ngx-bootstrap/bs-moment';
import {fr} from 'ngx-bootstrap/locale';

[...]

    public dpConfig: Partial<BsDatepickerConfig> = new BsDatepickerConfig();

    constructor(private _formBuilder: FormBuilder) {
        moment.locale('fr');
        defineLocale('fr', fr);
        this.Form = this._formBuilder.group({
            startDate: [this.startDate, Validators.required],
            endDate: [this.endDate, Validators.required]
        });

        this.dpConfig.containerClass = 'theme-blue';
        this.dpConfig.locale = 'fr';
        this.dpConfig.dateInputFormat = 'L'; // Or format like you want
    }

in component.html

    <div class="input-group" [formGroup]="Form">
        <input class="form-control"
               bsDatepicker
               id="startDate"
               type="text"
               [bsConfig]="dpConfig"
               formControlName="startDate"
               value="{{ startDate | date: 'dd/MM/yyyy' }}"
               [(ngModel)]="startDate">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
    </div>
    <div class="input-group" [formGroup]="Form">
        <input class="form-control"
               bsDatepicker
               id="endDate"
               type="text"
               [bsConfig]="dpConfig"
               formControlName="endDate"
               [minDate]="minEndDate"
               value="{{ endDate | date: 'dd/MM/yyyy' }}"
               [(ngModel)]="endDate">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
    </div>

@stiffold
Copy link

stiffold commented Oct 18, 2017

Yes this works! value="{{birthDate.value | date: 'dd/MM/yyyy'}}" birthDate is FormControl. Thx :)

@LordAlastair
Copy link

@alexisAvenel thank you!

@abhishekdgeek
Copy link

Any working workaround for template driven forms?

Thanks,
Abhishek.

@joaofsilva
Copy link

Above solution doesn't work for initial value?

@abhishekdgeek
Copy link

Yes @joaofsilva . :|

@gbendandi
Copy link

The solution above works for initial value but typing new date in it still handles the input as mm/dd/yyyy. Anyone found a workaround for that case?

@konasunny
Copy link

konasunny commented Nov 8, 2017

the below steps will solve the issue

  1. import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
  2. inject BsDatepickerConfig in constructor
  3. this._bsDatepickerConfig.dateInputFormat = 'DD MMM YYYY';

@gbendandi
Copy link

@konasunny This correctly formats an initial date or one selected from calendar according to selected locale, but when using the input field to type in a date its seems to only accept mm.dd.yyyy.

You can test this behaviour in the locales example here https://valor-software.com/ngx-bootstrap/#/datepicker

  1. select 'de' from locales dropdown.
  2. select date from datepicker calendar. Note dd.mm.yyy format.
  3. in the datepicker input type in 13.11.2017 (day > 12). Press enter.
  • expected: datepicker input should read 13.11.2017, 13th November 2017 should be selected on calendar.
  • actual: datepicker input is emptied, calendar selected date is not updated.
  1. in the datepicker input type in 12.10.2017 (day <= 12). Press enter.
  • expected: datepicker input should read 12.10.2017.
  • actual: datepicker input reads 10.12.2017. (dd & mm switched)

@felikf
Copy link

felikf commented Dec 19, 2017

Should not the typed-in date be parsed using provided date format from this.dpConfig.dateInputFormat ?

Instead of _locale.preparse(value), see bs-datepicker-input.directive.ts:

if (typeof value === 'string') {
      const date = new Date(_locale.preparse(value));
      this._value = isNaN(date.valueOf()) ? null : 
}

https://github.com/valor-software/ngx-bootstrap/blob/development/src/datepicker/bs-datepicker-input.directive.ts#L79

When I use @alexisAvenel solution I am still getting what @bendandi is describing (broken type in date format - not reflected in date picker).

image

@nathanchan01
Copy link

nathanchan01 commented Dec 20, 2017

It depends on what kind of form we use
For Template Driven Form: go @konasunny 's way
For Model Driven Or Reactive Forms: @alexisAvenel is correct

@experionakash
Copy link

Is this issue fixed for bsDateRangePicker?
value="{{birthDate.value | date: 'dd/MM/yyyy'}}"
I tried this and not worked for me because in case of date range picker birthDate contains an array of date objects.

@valorkin
Copy link
Member

You set date format for value via config or bsConfig property

@jonasmedeiros
Copy link

@valorkin when you add mask DD/MM/YYYY and add date by the calendar for example 11/02/1985 it works but when changing to the year to 2000 it goes for 02/11/1985 so there is a issue on focus out, is putting to the format of MM/DD/YYYY, so the only way this is working is to add DD/MMM/YYYY

@dottodot
Copy link

dottodot commented Feb 16, 2018

So how should dateInputFormat work? I've have the following bsConfig on a daterangepicker

    this.bsConfig = {
      containerClass: 'theme-blue',
      showWeekNumbers: false,
      dateInputFormat: 'DD/MM/YYYY'
    };

but it's still formatted as MM/DD/YYYY

@Prmleo
Copy link

Prmleo commented Apr 6, 2018

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

@manmohanagarwal4
Copy link

manmohanagarwal4 commented Jul 25, 2018

If you are using Date Range Picker then instead of "dateInputFormat" you should use "rangeInputFormat" in [bsConfig] .
I had fixed by this option .
Thanks .

@startswithaj
Copy link

Bump

@karthiikramesh
Copy link

@konasunny, thank you the above solution works for me

import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
inject BsDatepickerConfig in constructor
this._bsDatepickerConfig.dateInputFormat = 'YYYY-MM-DD';

@ujjaldey
Copy link

If you are using Date Range Picker then instead of "dateInputFormat" you should use "rangeInputFormat" in [bsConfig] .
I had fixed by this option .
Thanks .

Worked for me

@fabriciobatalha
Copy link

fabriciobatalha commented Jul 26, 2019

If you are using Date Range Picker then instead of "dateInputFormat" you should use "rangeInputFormat" in [bsConfig] .
I had fixed by this option .
Thanks .

It worked for me. Thanks, @manmohanagarwal4!

@afeef1915
Copy link

afeef1915 commented Jan 16, 2020

How to Fix Onchange Date after manually insert date date gets changed to different date.

for example

               10-May-1989  works for date format DD-MMM-YYYY
	main.js:2203 Wed May 10 1989 00:00:00 GMT+0530 (India Standard Time)
	14:11:40.317 main.js:2203 10-May-1989


	10/5/1989 but when manually change format it doesn' works.
	main.js:2203 Mon Jan 10 0005 00:00:00 GMT+0553 (India Standard Time)
	10-Jan-0005

it result different date.

code

 <input type="text" class="form-control" 
            bsDatepicker
            [formControlName]="field.code"
            [bsConfig]="datePickerConfig"
            [(ngModel)]="someDate"
            (ngModelChange)="testfunc($event)"
            placeholder='DD-MMM-YYYY'
            #dp="bsDatepicker"
            (keyup)="dp.toggle()"
           />

   function testfunc(search){


if (this.search!== '') {
  if (moment(search).isValid()) {
    this.someDate= moment(new Date(search)).format('DD-MMM-YYYY');
  } else {
    this.someDate= '';
  }

}

@PrashantSinghGour
Copy link

PrashantSinghGour commented Oct 11, 2023

for me issue is still there with bsDaterangepicker, it is working fine when we select the dates but while patching the value it is formatting to default MM/DD/YYYY.

this.bsConfig.rangeInputFormat = 'MM/YY';

any work around for this.

version - "ngx-bootstrap": "8.0.0"

Solution

I found simple workaround for this.

// get dateRangePicker Ref.
 @ViewChild(BsDaterangepickerDirective) bsDaterangepicker: BsDaterangepickerDirective;

// call setConfig after setting bsConfig.
this.bsConfig.rangeInputFormat = 'MM/YY';
   this.bsDaterangepicker.setConfig();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests