|
|
@@ -2,54 +2,54 @@ import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; |
|
|
import {FormBuilder, FormGroup} from "@angular/forms"; |
|
|
import {FormBuilder, FormGroup} from "@angular/forms"; |
|
|
|
|
|
|
|
|
@Component({ |
|
|
@Component({ |
|
|
selector: 'app-datetime-picker', |
|
|
|
|
|
templateUrl: './datetime-picker.component.html', |
|
|
|
|
|
styleUrl: './datetime-picker.component.scss' |
|
|
|
|
|
|
|
|
selector: 'app-datetime-picker', |
|
|
|
|
|
templateUrl: './datetime-picker.component.html', |
|
|
|
|
|
styleUrl: './datetime-picker.component.scss' |
|
|
}) |
|
|
}) |
|
|
export class DatetimePickerComponent implements OnInit { |
|
|
export class DatetimePickerComponent implements OnInit { |
|
|
@Input() label: string = 'Date and Time'; |
|
|
|
|
|
@Input() inputId: string = 'myId'; |
|
|
|
|
|
@Input() initialValue: string | null = null; |
|
|
|
|
|
@Input() readonly: boolean = false; |
|
|
|
|
|
@Input() showSeconds: boolean = false; |
|
|
|
|
|
@Output() dateTimeChange = new EventEmitter<string | null>(); |
|
|
|
|
|
|
|
|
@Input() label: string = 'Date and Time'; |
|
|
|
|
|
@Input() inputId: string = 'myId'; |
|
|
|
|
|
@Input() initialValue: string | null = null; |
|
|
|
|
|
@Input() readonly: boolean = false; |
|
|
|
|
|
@Input() showSeconds: boolean = false; |
|
|
|
|
|
@Output() dateTimeChange = new EventEmitter<string | null>(); |
|
|
|
|
|
|
|
|
form: FormGroup; |
|
|
|
|
|
|
|
|
form: FormGroup; |
|
|
|
|
|
|
|
|
constructor(private fb: FormBuilder) { |
|
|
|
|
|
this.form = this.fb.group({ |
|
|
|
|
|
date: [''], |
|
|
|
|
|
time: [''] |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnInit() { |
|
|
|
|
|
if (this.initialValue) { |
|
|
|
|
|
const date = new Date(this.initialValue); |
|
|
|
|
|
this.form.patchValue({ |
|
|
|
|
|
date: this.formatDate(date), |
|
|
|
|
|
time: this.formatTime(date) |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
constructor(private fb: FormBuilder) { |
|
|
|
|
|
this.form = this.fb.group({ |
|
|
|
|
|
date: [''], |
|
|
|
|
|
time: [''] |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (this.readonly) { |
|
|
|
|
|
this.form.disable(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
ngOnInit() { |
|
|
|
|
|
if (this.initialValue) { |
|
|
|
|
|
const date = new Date(this.initialValue); |
|
|
|
|
|
this.form.patchValue({ |
|
|
|
|
|
date: this.formatDate(date), |
|
|
|
|
|
time: this.formatTime(date) |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.form.valueChanges.subscribe(() => { |
|
|
|
|
|
if (!this.readonly) { |
|
|
|
|
|
this.emitDateTime(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (this.readonly) { |
|
|
|
|
|
this.form.disable(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private formatDate(date: Date): string { |
|
|
|
|
|
return date.toLocaleDateString('en-CA'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.form.valueChanges.subscribe(() => { |
|
|
|
|
|
if (!this.readonly) { |
|
|
|
|
|
this.emitDateTime(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private formatDate(date: Date): string { |
|
|
|
|
|
return date.toLocaleDateString('en-CA'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private formatTime(date: Date): string { |
|
|
private formatTime(date: Date): string { |
|
|
if (this.showSeconds) { |
|
|
if (this.showSeconds) { |
|
|
return date.toLocaleTimeString('en-GB', { hour12: false }); |
|
|
|
|
|
|
|
|
return date.toLocaleTimeString('en-GB', {hour12: false}); |
|
|
} else { |
|
|
} else { |
|
|
// Nur Stunden und Minuten zurückgeben |
|
|
// Nur Stunden und Minuten zurückgeben |
|
|
return date.toLocaleTimeString('en-GB', { |
|
|
return date.toLocaleTimeString('en-GB', { |
|
|
@@ -61,7 +61,7 @@ export class DatetimePickerComponent implements OnInit { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private emitDateTime() { |
|
|
private emitDateTime() { |
|
|
const { date, time } = this.form.value; |
|
|
|
|
|
|
|
|
const {date, time} = this.form.value; |
|
|
if (date && time) { |
|
|
if (date && time) { |
|
|
const [year, month, day] = date.split('-'); |
|
|
const [year, month, day] = date.split('-'); |
|
|
|
|
|
|
|
|
@@ -84,7 +84,7 @@ export class DatetimePickerComponent implements OnInit { |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
// Format the date to match the loaded format |
|
|
// Format the date to match the loaded format |
|
|
const formattedDate = dateTime.toLocaleString('sv-SE', { timeZone: 'Europe/Berlin' }).replace(' ', 'T') + '+02:00'; |
|
|
|
|
|
|
|
|
const formattedDate = dateTime.toLocaleString('sv-SE', {timeZone: 'Europe/Berlin'}).replace(' ', 'T') + '+02:00'; |
|
|
|
|
|
|
|
|
this.dateTimeChange.emit(formattedDate); |
|
|
this.dateTimeChange.emit(formattedDate); |
|
|
} else { |
|
|
} else { |
|
|
|