Flowbite date-range-picker: How to get selected dates? (angular)

I have implemented the Flowbite date-range-picker in my UI, but I am unable to fetch the chosen dates from the input fields.

I have tried:

  1. The change event: (change)=”onStartDateChange($event)”
  2. Adding event listener on element using ViewChild/elementRef

Interestingly, the event listener using ViewChild/elementRef does work if the event is ‘click’ (but then it has wrong behavior), but does not work when the event is ‘change’

TS:

import { Component, Output, EventEmitter, AfterViewInit, ViewChild, ElementRef } from 

'@angular/core';
import { TranslateService } from '@ngx-translate/core';


@Component({
  selector: 'date-range-picker',
  templateUrl: './date-range-picker.component.html',
  styleUrls: ['./date-range-picker.component.scss'],
})
export class DateRangePickerComponent {
  startDate: Date | null = null;
  endDate: Date | null = null;

  @Output() dateRangeSelected = new EventEmitter<{ startDate: Date; endDate: Date; }>();

  @ViewChild('datepickerRangeStart', { static: false }) startDateInput!: ElementRef;
  @ViewChild('datepickerRangeEnd', { static: false }) endDateInput!: ElementRef;

  constructor(private translate: TranslateService) {}

  onStartDateChange(event: any): void {
    console.log('--- Start date change event:', event);
    this.startDate = event.target.value ? new Date(event.target.value) : null;
    this.emitDateRangeIfComplete();
  }

  onEndDateChange(event: any): void {
    console.log('--- End date change event:', event);
    this.endDate = event.target.value ? new Date(event.target.value) : null;
    this.emitDateRangeIfComplete();
  }

  // Emits only when both start and end date are chosen
  private emitDateRangeIfComplete(): void {
    if (this.startDate && this.endDate) {
      this.dateRangeSelected.emit({
        startDate: this.startDate,
        endDate: this.endDate,
      });
      console.log('-- Emit function runs');
    }
  }

  ngAfterViewInit() {
    this.startDateInput.nativeElement.addEventListener('change', (e: Event) => {
      const input = e.target as HTMLInputElement;
      console.log('Start date is: ' + input.value);
    });

    this.endDateInput.nativeElement.addEventListener('change', (e: Event) => {
      const input = e.target as HTMLInputElement;
      console.log('End date is: ' + input.value);
    });
  }
}

HTML:

<div id="date-range-picker" date-rangepicker>
  <div class="relative">
    <div
      class="absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none"
    >
      <i class="ci-Calendar_Days text-lg"></i>
    </div>
    <input
      #datepickerRangeStart
      id="datepicker-range-start"
      name="start"
      [(ngModel)]="startDate"
      (change)="onStartDateChange($event)"
      class="bg-gray-50 border text-gray-900 text-sm rounded-lg block ps-10 p-2.5 cursor-pointer"
      placeholder="{{ 'Select start date' | translate }}"
      autocomplete="off"
      readonly
    />
  </div>
  <span class="mx-2 text-gray-500">{{ "to" | translate }}</span>
  <div class="relative">
    <div
      class="absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none"
    >
      <i class="ci-Calendar_Days text-lg"></i>
    </div>
    <input
      #datepickerRangeEnd
      id="datepicker-range-end"
      name="end"
      [(ngModel)]="endDate"
      (change)="onEndDateChange($event)"
      class="bg-gray-50 border text-gray-900 text-sm rounded-lg block ps-10 p-2.5 cursor-pointer"
      placeholder="{{ 'Select end date' | translate }}"
      autocomplete="off"
      readonly
    />
  </div>
</div>

From this GitHub comment, use changeDate event listener/binding instead of change.

<input
    #datepickerRangeStart
    id="datepicker-range-start"
    name="start"
    [(ngModel)]="startDate"
    (changeDate)="onStartDateChange($event)"
    class="bg-gray-50 border text-gray-900 text-sm rounded-lg block ps-10 p-2.5 cursor-pointer"
    placeholder="{{ 'Select start date' }}"
    autocomplete="off"
    readonly
  />

<input
    #datepickerRangeEnd
    id="datepicker-range-end"
    name="end"
    [(ngModel)]="endDate"
    (changeDate)="onEndDateChange($event)"
    class="bg-gray-50 border text-gray-900 text-sm rounded-lg block ps-10 p-2.5 cursor-pointer"
    placeholder="{{ 'Select end date' }}"
    autocomplete="off"
    readonly
  />

Demo @ StackBlitz

Instead of flowbite which does not cater to angular two way bindings and other features, you should consider flowbite-angular

I am aware that it does not have a datepicker yet available, but it will be present in the future.


Would recommend you choose a more mature UI library until flowbite-angular is good to go for angular. Currently it’s two early and will just give you headaches, writing plain javascript for listeners using flowbite (as seen in the question), this eliminates the advantages and power that angular brings to the table.


At the time of answering this question, flowbite-angular is early stage, but in future it should be readily available for use.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật