1 import {Component, OnInit, Input, Output, ViewChild, EventEmitter} from '@angular/core';
2 import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
5 * RE: displaying locale dates in the input field:
6 * https://github.com/ng-bootstrap/ng-bootstrap/issues/754
7 * https://stackoverflow.com/questions/40664523/angular2-ngbdatepicker-how-to-format-date-in-inputfield
11 selector: 'eg-date-select',
12 templateUrl: './date-select.component.html'
14 export class DateSelectComponent implements OnInit {
16 @Input() initialIso: string; // ISO string
17 @Input() initialYmd: string; // YYYY-MM-DD (uses local time zone)
18 @Input() initialDate: Date; // Date object
19 @Input() required: boolean;
20 @Input() fieldName: string;
24 @Input() set disabled(d: boolean) {
28 current: NgbDateStruct;
30 @Output() onChangeAsDate: EventEmitter<Date>;
31 @Output() onChangeAsIso: EventEmitter<string>;
32 @Output() onChangeAsYmd: EventEmitter<string>;
35 this.onChangeAsDate = new EventEmitter<Date>();
36 this.onChangeAsIso = new EventEmitter<string>();
37 this.onChangeAsYmd = new EventEmitter<string>();
42 if (this.initialYmd) {
43 this.initialDate = this.localDateFromYmd(this.initialYmd);
45 } else if (this.initialIso) {
46 this.initialDate = new Date(this.initialIso);
49 if (this.initialDate) {
51 year: this.initialDate.getFullYear(),
52 month: this.initialDate.getMonth() + 1,
53 day: this.initialDate.getDate()
59 const ymd = `${evt.year}-${evt.month}-${evt.day}`;
60 const date = this.localDateFromYmd(ymd);
61 const iso = date.toISOString();
62 this.onChangeAsDate.emit(date);
63 this.onChangeAsYmd.emit(ymd);
64 this.onChangeAsIso.emit(iso);
67 // Create a date in the local time zone with selected YMD values.
68 // TODO: Consider moving this to a date service...
69 localDateFromYmd(ymd: string): Date {
70 const parts = ymd.split('-');
72 Number(parts[0]), Number(parts[1]) - 1, Number(parts[2]));