Skip to content

Commit

Permalink
fix(datepicker): date parsing when using a custom date format (#3522)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi authored and valorkin committed Jan 24, 2018
1 parent 8fe43e2 commit 5c2aa9e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
12 changes: 12 additions & 0 deletions demo/src/app/components/+datepicker/datepicker-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
NgApiDocComponent,
NgApiDocConfigComponent
} from '../../docs/api-docs';
import { DemoDatePickerCustomFormatComponent } from './demos/custom-format/date-picker-custom-format';

export const demoComponentContent: ContentSection[] = [
{
Expand Down Expand Up @@ -51,6 +52,17 @@ export const demoComponentContent: ContentSection[] = [
`,
outlet: DemoDatePickerPopupComponent
},
{
title: 'Custom date format',
anchor: 'format',
component: require('!!raw-loader?lang=typescript!./demos/custom-format/date-picker-custom-format.ts'),
html: require('!!raw-loader?lang=markup!./demos/custom-format/date-picker-custom-format.html'),
description: `
<p>You can easily change the date format by specifying the <code>dateInputFormat</code> in <code>[bsConfig]</code></p>
<p>The following example shows how to use a datepicker having <code>YYYY-MM-DD</code> date format inside a form:</p>
`,
outlet: DemoDatePickerCustomFormatComponent
},
{
title: 'Themes',
anchor: 'themes',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<form [formGroup]="myForm">
<input
class="form-control"
[minDate]="minDate"
[maxDate]="maxDate"
#dp="bsDatepicker"
bsDatepicker
formControlName="myDate"
[bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }"
>
</form>
</div>
<div class="col-xs-12 col-12 col-md-3 form-group">
<button class="btn btn-success" (click)="dp.toggle()">Date Picker</button>
</div>
</div>
<br>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
selector: 'demo-date-picker-custom-format',
templateUrl: './date-picker-custom-format.html'
})
export class DemoDatePickerCustomFormatComponent {
minDate = new Date(2017, 5, 10);
maxDate = new Date(2018, 9, 15);

myForm = new FormGroup({
myDate: new FormControl(new Date())
});
}
2 changes: 2 additions & 0 deletions demo/src/app/components/+datepicker/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { DemoDatepickerMinMaxComponent } from './min-max/min-max.component';
import { DemoDatepickerDisabledComponent } from './disabled/disabled.component';
import { DemoDatepickerFormsComponent } from './forms/forms.component';
import { DemoDatepickerReactiveFormsComponent } from './reactive-forms/reactive-forms.component';
import { DemoDatePickerCustomFormatComponent } from './custom-format/date-picker-custom-format';

export const DEMO_COMPONENTS = [
DatepickerDemoComponent,
DemoDatePickerPopupComponent,
DemoDatePickerCustomFormatComponent,
DemoDatepickerColorThemingComponent,
DemoDatepickerChangeLocaleComponent,
DemoDatepickerMinMaxComponent,
Expand Down
3 changes: 1 addition & 2 deletions src/datepicker/bs-datepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class BsDatepickerInputDirective
private _value: Date;

constructor(@Host() private _picker: BsDatepickerDirective,
private _config: BsDatepickerConfig,
private _localeService: BsLocaleService,
private _renderer: Renderer2,
private _elRef: ElementRef,
Expand Down Expand Up @@ -115,7 +114,7 @@ export class BsDatepickerInputDirective
`Locale "${_localeKey}" is not defined, please add it with "defineLocale(...)"`
);
}
this._value = parseDate(value, this._config.dateInputFormat, this._localeService.currentLocale);
this._value = parseDate(value, this._picker._config.dateInputFormat, this._localeService.currentLocale);
}

this._picker.bsValue = this._value;
Expand Down
3 changes: 1 addition & 2 deletions src/datepicker/bs-daterangepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class BsDaterangepickerInputDirective
private _value: Date[];

constructor(@Host() private _picker: BsDaterangepickerDirective,
private _config: BsDatepickerConfig,
private _localeService: BsLocaleService,
private _renderer: Renderer2,
private _elRef: ElementRef,
Expand Down Expand Up @@ -142,7 +141,7 @@ export class BsDaterangepickerInputDirective

this._value = (_input as string[])
.map((_val: string): Date =>
parseDate(_val, this._config.dateInputFormat, this._localeService.currentLocale))
parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale))
.map((date: Date) => (isNaN(date.valueOf()) ? null : date));
}

Expand Down

0 comments on commit 5c2aa9e

Please sign in to comment.