1 import { Component, Input, OnInit } from '@angular/core';
2 import { UntypedFormControl } from '@angular/forms';
4 import { NgbCalendar, NgbDateStruct, NgbTimeStruct } from '@ng-bootstrap/ng-bootstrap';
5 import moment from 'moment';
6 import { Subscription } from 'rxjs';
9 selector: 'cd-date-time-picker',
10 templateUrl: './date-time-picker.component.html',
11 styleUrls: ['./date-time-picker.component.scss']
13 export class DateTimePickerComponent implements OnInit {
15 control: UntypedFormControl;
24 minDate: NgbDateStruct;
30 constructor(private calendar: NgbCalendar) {}
33 this.minDate = this.calendar.getToday();
35 this.format = 'YYYY-MM-DD';
36 } else if (this.hasSeconds) {
37 this.format = 'YYYY-MM-DD HH:mm:ss';
39 this.format = 'YYYY-MM-DD HH:mm';
42 let mom = moment(this.control?.value, this.format);
44 if (!mom.isValid() || mom.isBefore(moment())) {
48 this.date = { year: mom.year(), month: mom.month() + 1, day: mom.date() };
49 this.time = { hour: mom.hour(), minute: mom.minute(), second: mom.second() };
56 const datetime = Object.assign({}, this.date, this.time);
59 this.control.setValue(moment(datetime).format(this.format));
63 this.control.setValue('');