class="form-control"
[placeholder]="initialIso"
[(ngModel)]="stringVersion"
- (blur)="blurred($event)"
+ (blur)="handleInputChanged($event)"
+ (change)="handleInputChanged($event)"
#dtPicker="ngbPopover"
[ngbPopover]="dt"
placement="bottom"
</div>
<ngb-datepicker
[(ngModel)]="dateModel"
- (ngModelChange)="modelChanged()"
+ (select)="handleDateChanged($event)"
[footerTemplate]="time">
</ngb-datepicker>
</div>
<ng-template #time>
<ngb-timepicker name="time"
[(ngModel)]="timeModel" [meridian]="true"
- (ngModelChange)="modelChanged()"
+ (ngModelChange)="handleTimeChanged()"
[spinners]="true"
[hourStep]="1"
[minuteStep]="minuteStep || 30" >
<span *ngIf="showTZ" class="badge badge-info">{{ timezone || 'America/Los_Angeles'}}</span>
<button i18n class="btn btn-success" (click)="dtPicker.close()">Choose time</button>
</ng-template>
+
}
ngOnInit() {
- const start = this.initialIso ? Moment(this.initialIso, this.timezone) : Moment(new Date(), this.timezone);
- this.setDefaultDate(start);
- this.setDefaultTime(start);
+ const start = this.initialIso ? Moment.tz(this.initialIso, Moment.ISO_8601, this.timezone) : Moment.tz([], this.timezone);
+ this.stringVersion = this.format.transform({value: start, datatype: 'timestamp', datePlusTime: true});
+ this.setDatePicker(start);
+ this.setTimePicker(start);
- if (this.initialIso) {
- this.modelChanged(null);
- }
}
- setDefaultDate(start: any) {
- this.dateModel = { year: start.year(), month: start.month() + 1, day: start.date() };
+ setDatePicker(current: Moment) {
+ this.dateModel = {
+ year: current.tz(this.timezone).year(),
+ month: current.tz(this.timezone).month() + 1,
+ day: current.tz(this.timezone).date() };
}
- setDefaultTime(start: any) {
- const remainder = this.minuteStep - start.minute() % this.minuteStep,
- final = Moment(start, this.timezone).add(remainder, 'minutes');
-
- // Seed time model with current, rounding up to nearest minuteStep (does roll hour over if needed)
- this.timeModel = { hour: final.hour(), minute: final.minute(), second: 0 };
+ setTimePicker(current: Moment) {
+ this.timeModel = {
+ hour: current.tz(this.timezone).hour(),
+ minute: current.tz(this.timezone).minute(),
+ second: 0 };
}
+ handleDateChanged($event) {
+ const newDate = Moment.tz([$event.year, ($event.month - 1), $event.day,
+ this.timeModel.hour, this.timeModel.minute, 0], this.timezone);
+ this.stringVersion = this.format.transform({value: newDate, datatype: 'timestamp', datePlusTime: true});
+ this.onChangeAsMoment.emit(newDate);
+ this.onChangeAsIso.emit(newDate.toISOString());
+ }
- blurred(event) {
- this.modelChanged(event);
+ handleTimeChanged(event) {
+ const newDate = Moment.tz([this.dateModel.year, (this.dateModel.month - 1), this.dateModel.day,
+ this.timeModel.hour, this.timeModel.minute, 0], this.timezone);
+ this.stringVersion = this.format.transform({value: newDate, datatype: 'timestamp', datePlusTime: true});
+ this.onChangeAsMoment.emit(newDate);
+ this.onChangeAsIso.emit(newDate.toISOString());
}
- modelChanged(event) {
+ handleInputChanged(event) {
let newDate: Moment;
- if (event) { // if the change came from the input field
+ if (event) {
newDate = this.format.momentizeDateTimeString(this.stringVersion, this.timezone);
- } else {
- newDate = Moment(new Date(this.dateModel.year, (this.dateModel.month - 1), this.dateModel.day,
- this.timeModel.hour, this.timeModel.minute, this.timeModel.second), this.timezone);
- }
-
- if (newDate && !isNaN(newDate)) {
- // Set the input field to the current value
- this.stringVersion = this.format.transform({value: newDate, datatype: 'timestamp', datePlusTime: true});
- // Update form passed in view value
+ this.setDatePicker(newDate);
+ this.setTimePicker(newDate);
this.onChangeAsMoment.emit(newDate);
this.onChangeAsIso.emit(newDate.toISOString());
}