
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
mydatepicker
Advanced tools
New datepicker library
If your Angular version is >= 7.2 you can use a new version (angular-mydatepicker) of this component:
Version compatibility of this library
Library version | Angular version | Branch | Installation |
---|---|---|---|
< 9.0.0 | >= 2 and < 9 | master | npm install --save mydatepicker@latest |
>= 9.0.0 | >= 9 | angularIvy | npm install --save mydatepicker@ng9 |
Angular date picker
Highly configurable Angular date picker. Compatible Angular2+.
Online demo is here
If you want to set own styles to the input box, the calendar and the clear buttons you can try this attribute directive date picker.
To install this component to an external project, follow the procedure:
npm install mydatepicker --save
Add MyDatePickerModule import to your @NgModule like example below
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { MyTestApp } from './my-test-app';
import { MyDatePickerModule } from 'mydatepicker';
@NgModule({
imports: [ BrowserModule, FormsModule, MyDatePickerModule ],
declarations: [ MyTestApp ],
bootstrap: [ MyTestApp ]
})
export class MyTestAppModule {}
Use one of the following three options.
In this option the ngModel binding is used. Here is an example application. It shows how to use the ngModel.
To use ngModel define the application class as follows:
import {IMyDpOptions} from 'mydatepicker';
// other imports here...
export class MyTestApp {
public myDatePickerOptions: IMyDpOptions = {
// other options...
dateFormat: 'dd.mm.yyyy',
};
// Initialized to specific date (09.10.2018).
public model: any = { date: { year: 2018, month: 10, day: 9 } };
constructor() { }
}
Add the following snippet inside your template:
<form #myForm="ngForm" novalidate>
<my-date-picker name="mydate" [options]="myDatePickerOptions"
[(ngModel)]="model" required></my-date-picker>
</form>
In this option the value accessor of reactive forms is used. Here is an example application. It shows how to use the formControlName.
To use reactive forms define the application class as follows:
import {IMyDpOptions} from 'mydatepicker';
// other imports here...
export class MyTestApp implements OnInit {
public myDatePickerOptions: IMyDpOptions = {
// other options...
dateFormat: 'dd.mm.yyyy',
};
public myForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.myForm = this.formBuilder.group({
// Empty string or null means no initial value. Can be also specific date for
// example: {date: {year: 2018, month: 10, day: 9}} which sets this date to initial
// value.
myDate: [null, Validators.required]
// other controls are here...
});
}
setDate(): void {
// Set today date using the patchValue function
let date = new Date();
this.myForm.patchValue({myDate: {
date: {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate()}
}});
}
clearDate(): void {
// Clear the date using the patchValue function
this.myForm.patchValue({myDate: null});
}
}
Add the following snippet inside your template:
<form [formGroup]="myForm" novalidate>
<my-date-picker name="mydate" [options]="myDatePickerOptions"
formControlName="myDate"></my-date-picker>
<!-- other controls are here... -->
</form>
In this option the mydatepicker sends data back to host application using callbacks. Here is an example application. It shows how to use callbacks.
To use callbacks define the application class as follows:
import {IMyDpOptions, IMyDateModel} from 'mydatepicker';
// other imports here...
export class MyTestApp {
myDatePickerOptions: IMyDpOptions = {
// other options...
dateFormat: 'dd.mm.yyyy',
};
constructor() { }
// dateChanged callback function called when the user select the date. This is mandatory callback
// in this option. There are also optional inputFieldChanged and calendarViewChanged callbacks.
onDateChanged(event: IMyDateModel) {
// event properties are: event.date, event.jsdate, event.formatted and event.epoc
}
}
Add the following snippet inside your template:
<my-date-picker [options]="myDatePickerOptions"
(dateChanged)="onDateChanged($event)"></my-date-picker>
Value of the options attribute is a type of IMyDpOptions. It can contain the following properties.
Option | Default | Type | Description |
---|---|---|---|
dayLabels | {su: 'Sun', mo: 'Mon', tu: 'Tue', we: 'Wed', th: 'Thu', fr: 'Fri', sa: 'Sat'} | IMyDayLabels | Day labels visible on the selector. |
monthLabels | { 1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec' } | IMyMonthLabels | Month labels visible on the selector. |
dateFormat | yyyy-mm-dd | string | Date format on the selection area and the callback. For example: d.m.yyyy, dd.mm.yyyy, yyyy-m-d, yyyy-mm-dd, d mmm yyyy, dd mmm yyyy (d = Day not leading zero, dd = Day with leading zero, m = Month not leading zero, mm = Month with leading zero, mmm = Month as a text, yyyy = Year four digit) |
showTodayBtn | true | boolean | Show 'Today' button on calendar. |
todayBtnTxt | Today | string | Today button text. Can be used if showTodayBtn = true. |
firstDayOfWeek | mo | string | First day of week on calendar. One of the following: mo, tu, we, th, fr, sa, su |
sunHighlight | true | boolean | Sunday red colored on calendar. |
satHighlight | false | boolean | Saturday red colored on calendar. |
highlightDates | no default value | Array<IMyDate> | Dates red colored on calendar. For example: [{year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}] |
markCurrentDay | true | boolean | Is current day (today) marked on calendar. |
markCurrentMonth | true | boolean | Is current month marked on calendar. Can be used if monthSelector = true. |
markCurrentYear | true | boolean | Is current year marked on calendar. Can be used if yearSelector = true. |
monthSelector | true | boolean | If month label is selected opens a selector of months. |
yearSelector | true | boolean | If year label is selected opens a selector of years. |
minYear | 1000 | number | Minimum allowed year in calendar. Cannot be less than 1000. |
maxYear | 9999 | number | Maximum allowed year in calendar. Cannot be more than 9999. |
disableUntil | no default value | IMyDate | Disable dates backward starting from the given date. For example: {year: 2016, month: 6, day: 26}. To reset existing disableUntil value set: {year: 0, month: 0, day: 0} |
disableSince | no default value | IMyDate | Disable dates forward starting from the given date. For example: {year: 2016, month: 7, day: 22}. To reset existing disableSince value set: {year: 0, month: 0, day: 0} |
disableDays | no default value | Array<IMyDate> | Disable single dates one by one. Array of disabled dates. For example: [{year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}]. To reset existing disableDays value set empty array to it. |
enableDays | no default value | Array<IMyDate> | Enable given dates one by one if the date is disabled. For example if you disable the date range and want to enable some dates in range. Array of enabled days. For example: [{year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}]. To reset existing enableDays value set empty array to it. |
disableDateRanges | no default value | Array<IMyDateRange> | Disable date ranges. For example: [{begin: {year: 2016, month: 11, day: 14}, end: {year: 2016, month: 11, day: 20}}]. To reset existing disableDateRanges value set empty array to it. |
disableWeekends | false | boolean | Disable weekends (Saturday and Sunday). |
disableWeekdays | no default value | Array< string > | Disable weekdays. Array of weekdays to disable. Weekdays are same strings as the firstDayOfWeek option. For example: ['tu', 'we'] which disables Tuesdays and Wednesdays. |
markDates | no default value | Array<IMyMarkedDates> | Mark dates for different colors. For example: [{dates: [{year: 2016, month: 11, day: 14}, {year: 2016, month: 12, day: 16}], color: '#004198'}, {dates: [{year: 2017, month: 10, day: 1}, {year: 2017, month: 11, day: 4}], color: 'green'}]. To reset existing markDates value set empty array to it. |
markWeekends | no default value | IMyMarkedDate | Mark weekends (Saturday and Sunday). For example: {marked: true, color: 'red'}. Value of color can be any CSS color code. To reset existing markWeekends set: {marked: false, color: ''} |
disableHeaderButtons | true | boolean | Prevent to change the calendar view with header buttons if previous or next month are fully disabled by disableUntil or disableSince. |
showWeekNumbers | false | boolean | Are week numbers visible or not on calendar. Can be used if firstDayOfWeek = mo. |
selectorHeight | 232px | string | Selector height. |
selectorWidth | 252px | string | Selector width. |
allowDeselectDate | false | boolean | Is deselect of selected date allowed or not. |
inline | false | boolean | Show mydatepicker in inline mode. |
showClearDateBtn | true | boolean | Is clear date button shown or not. Can be used if inline = false. |
showDecreaseDateBtn | false | boolean | Is decrease date button shown or not. Can be used if inline = false. |
showIncreaseDateBtn | false | boolean | Is increase date button shown or not. Can be used if inline = false. |
height | 34px | string | mydatepicker height in without selector. Can be used if inline = false. |
width | 100% | string | mydatepicker width. Can be used if inline = false. |
selectionTxtFontSize | 14px | string | Selection area font size. Can be used if inline = false. |
alignSelectorRight | false | boolean | Align selector right. Can be used if inline = false. |
openSelectorTopOfInput | false | boolean | Open selector top of input field. The selector arrow cannot be shown if this option is true. Can be used if inline = false. |
indicateInvalidDate | true | boolean | If user typed date is not same format as dateFormat, show red background in the selection area. Can be used if inline = false. |
componentDisabled | false | boolean | Is selection area input field and buttons disabled or not (input disabled flag). You can also disable component by disabled attribute. Can be used if inline = false. |
editableDateField | true | boolean | Is selection area input field editable or not (input readonly flag). Can be used if inline = false. |
showSelectorArrow | true | boolean | Is selector (calendar) arrow shown or not. Can be used if inline = false. |
showInputField | true | boolean | Is selection area input field shown or not. If not, just show the icon. Can be used if inline = false. |
openSelectorOnInputClick | false | boolean | Open selector when the input field is clicked. Can be used if inline = false and editableDateField = false. |
allowSelectionOnlyInCurrentMonth | true | boolean | Is a date selection allowed or not other than current month. |
ariaLabelInputField | Date input field | string | Aria label text of input field. |
ariaLabelClearDate | Clear Date | string | Aria label text of clear date button. |
ariaLabelDecreaseDate | Decrease Date | string | Aria label text of decrease date button. |
ariaLabelIncreaseDate | Increase Date | string | Aria label text of increase date button. |
ariaLabelOpenCalendar | Open Calendar | string | Aria label text of open calendar button. |
ariaLabelPrevMonth | Previous Month | string | Aria label text of previous month button. |
ariaLabelNextMonth | Next Month | string | Aria label text of next month button. |
ariaLabelPrevYear | Previous Year | string | Aria label text of previous year button. |
ariaLabelNextYear | Next Year | string | Aria label text of next year button. |
ariaLabelDay | Days | string | Aria label text of day button. |
myDatePickerOptions: IMyDpOptions = {
todayBtnTxt: 'Today',
dateFormat: 'yyyy-mm-dd',
firstDayOfWeek: 'mo',
sunHighlight: true,
inline: false,
disableUntil: {year: 2016, month: 8, day: 10}
};
An ISO 639-1 language code can be provided as shorthand for the following options (dayLabels, monthLabels, dateFormat, todayBtnTxt, firstDayOfWeek and sunHighlight). Currently supported languages: en, fr, fr-ch, ja, fi, es, hu, sv, nl, ru, uk, no, tr, pt-br, de, de-ch, it, it-ch, pl, my, sk, sl, zh-cn, he, ro, ca, id, en-au, am-et, cs, el, kk, th, ko-kr, da, lt, vi, bn, bg, hr, ar, is, tw, lv and et.
The locale options can be override by options attribute.
If you would prefer a different year and month to be the default for a freshly chosen date picking operation, specify a defaultMonth attribute.
Value of the defaultMonth attribute can be:
Here is an example on how to use this attribute.
Placeholder text in the input field. Here is an example on how to use this attribute.
Boolean value indicating is the component disabled or not. Here is an example on how to use this attribute.
Selector can be opened or closed using this attribute. Value of the selector attribute can be:
Here is an example on how to use this attribute. Another way is to call a function of mydatepicker. Here is an example.
called when the date is selected, removed or input field typing is valid
event parameter:
event parameter type is IMyDateModel
Example of the dateChanged callback:
onDateChanged(event: IMyDateModel) {
console.log('onDateChanged(): ', event.date, ' - jsdate: ', new Date(event.jsdate).toLocaleDateString(), ' - formatted: ', event.formatted, ' - epoc timestamp: ', event.epoc);
}
called when the value change in the input field, date is selected or date is cleared (can be used in validation, returns true or false indicating is date valid or not in the input field)
event parameter:
event parameter type is IMyInputFieldChanged
Example of the input field changed callback:
onInputFieldChanged(event: IMyInputFieldChanged) {
console.log('onInputFieldChanged(): Value: ', event.value, ' - dateFormat: ', event.dateFormat, ' - valid: ', event.valid);
}
called when the calendar view change (year or month change)
event parameter:
event parameter type is IMyCalendarViewChanged
values of the weekday property are same as values of the firstDayOfWeek option
Example of the calendar view changed callback:
onCalendarViewChanged(event: IMyCalendarViewChanged) {
console.log('onCalendarViewChanged(): Year: ', event.year, ' - month: ', event.month, ' - first: ', event.first, ' - last: ', event.last);
}
called when the calendar is opened or closed
Example of the calendar toggle callback:
onCalendarToggle(event: number): void {
console.log('onCalendarClosed(): Reason: ', event);
}
called when the input box get or lost focus
event parameter:
event parameter type is IMyInputFocusBlur
Example of the input focus blur callback:
onInputFocusBlur(event: IMyInputFocusBlur): void {
console.log('onInputFocusBlur(): Reason: ', event. reason, ' - Value: ', event.value);
}
The styles of the component can be changed by overriding the styles.
Create a separate stylesheet file which contain the changed styles. Then import the stylesheet file in the place which is after the place where the component is loaded.
The sampleapp of the component contain an example:
At first fork and clone this repo.
Install all dependencies:
Build the npmdist folder and execute tslint:
Execute unit tests and coverage (output is generated to the test-output folder):
Run sample application:
Build a local npm installation package:
Install local npm package to your project:
Online demo is here
FAQs
Angular date picker
The npm package mydatepicker receives a total of 6,143 weekly downloads. As such, mydatepicker popularity was classified as popular.
We found that mydatepicker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.