I've been looking around for a way to mark up special days in the calendar to the user. I know that you can disable certain days in the datepicker but I would like to be able to just mark/style certain days, for instance change background/color to red on dates that have a special value, holidays for instance, and not disable them.
I have not found any way to do this, sorry if I have missed something obvious. Have anyone found a way to do this or is it easier to just make my own datepicker in this case?
If you look at the HTML of an open datepicker, you will see each day is represented by a
td
, like so:What we can derive from that HTML is an
aria-label
and a class ofmat-calendar-body-cell
.To achieve what you want, you can use the
@Output() openedStream
event. This will be triggered whenever the calendar is opened.As per the docs:
@Output('opened') openedStream: EventEmitter<void>
.Once you receive the event, you can then query the DOM by the
mat-calendar-body-cell
class that has a combination of anaria-label
attribute with a certain date, and if there are matches then you can add a class to those matches.Let me know if you have any issues with that. But you should be able to follow that rubric step by step to achieve what you want.
For more options from the datepicker API, look at the docs here.
mat-datepicker
has adateClass
input that does exactly this. Pass it a function that accepts aDate
and return a class name to apply.Example from docs:
Note in my case I had to apply the background to
.mat-calendar-body-cell-content
, a direct child of.example-custom-date-class
, rather than to the class directly as in the example.html:
.ts:
this working fine in my code. i hope work to you.
thanks, Abdullah