ng-gregorian-hijri-datepicker
Version:
* Lightweight **Angular** datepicker based on [ng-bootstrap](https://ng-bootstrap.github.io/#/components/datepicker/overview) that supports **Gregorian** and **Hijri** calendars. * Provides ability to swap between **Gregorian** and **Hijri** calendars *
103 lines (78 loc) • 2.64 kB
text/typescript
import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import {NgbDateStruct, NgbDatepicker} from '@ng-bootstrap/ng-bootstrap';
import { DateType } from './consts';
import { DateFormatterService } from './date-formatter.service';
import momentjs from 'moment';
const moment = momentjs;
import moment_ from 'moment-hijri';
import { NgForm, ControlContainer } from '@angular/forms';
const momentHijri = moment_;
export class HijriGregorianDatepickerComponent implements OnInit {
datePicker: any;
selectedDateType: DateType;
selectedDate: NgbDateStruct;
selectedDateChange: EventEmitter<NgbDateStruct> = new EventEmitter();
label: string;
showLabel = true;
readonly = false;
isRequired = false;
disabled = false;
minHijri: NgbDateStruct;
maxHijri: NgbDateStruct;
minGreg: NgbDateStruct;
maxGreg: NgbDateStruct;
hijriLabel: string;
GregLabel: string;
placeHolder: string;
get DateType() {
return DateType;
}
constructor( private dateFormatterService: DateFormatterService) { }
ngOnInit() {
if (!this.selectedDateType) {
this.selectedDateType = DateType.Hijri;
}
}
close() {
this.datePicker.close();
}
clear() {
this.selectedDate = undefined;
this.close();
this.selectedDateChange.emit(null);
}
getSelectedDate(): string {
let formattedDate = this.dateFormatterService.ToString(this.selectedDate);
if (this.selectedDateType == DateType.Hijri) {
return momentHijri(formattedDate, 'iD/iM/iYYYY').locale('en').format();
}
if (this.selectedDateType == DateType.Gregorian) {
return moment(formattedDate, 'D/M/YYYY').locale('en').format();
}
}
dateSelected() {
this.selectedDateChange.emit(this.selectedDate);
}
hijriClick() {
if (this.selectedDateType == DateType.Hijri) {
return;
}
this.selectedDateType = DateType.Hijri;
//to hijri
this.selectedDate = this.dateFormatterService.ToHijri(this.selectedDate);
this.selectedDateChange.emit(this.selectedDate);
}
gregClick() {
if (this.selectedDateType == DateType.Gregorian) {
return;
}
this.selectedDateType = DateType.Gregorian;
//to Gregorian
this.selectedDate = this.dateFormatterService.ToGregorian(this.selectedDate);
this.selectedDateChange.emit(this.selectedDate);
}
}