Repository navigation
Description
Bug, feature request, or proposal:
Feature
What is the expected behavior?
User being able to select a month in a year, or just a full year number. The component already have a startView attribute. It could have also a view attribute so the component could be configured to show just months of the year, or years.
What is the current behavior?
The user can select a full date only (day, month and year).
What is the use-case or motivation for changing an existing behavior?
Although the design docs does mention that will be a date & time option in the future, it does not mention this feature (at least, not explicitly). It's sometimes useful to set the view of a datepicker to allow the selection of just a month or a year. Maybe the problem is related to the date object, that demands a complete date. But it can be left to the developer the control of what is being grabbed from the datepicker, for example:
- if in year view mode and the user selects july/2017, the component parse it to the first day of july. Something like new Date(2017,6,1);
- if in "decades" view mode and the user selects the year of 2017, the component parse it to the first day of 2017. Something like new Date(2017,0,1).
The developer would have to know how he configured the component and what kind of information it'll give back to him.
The current state of the component can be used for the user to select a month and a year, but it's too confusing to the user if I put a full calendar to choosing just a month (along with some information like "choose any day in the desired month").
A work around to the lack of this feature is to use a regular mdInput with type="number" (that's the way I use for now, because, after all, representing months as numbers are common knowledge). But it's not a nice way because the user must select (in case of months) the month and the year in separate controls.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
all
Is there anything else we should know?
no
Activity
fxck commentedon May 29, 2017
it will be added eventually #675 (comment) (and following discussion)
mmalerba commentedon Nov 21, 2017
dup of #5845
mmalerba commentedon Nov 21, 2017
oh whoops, misread this, not a dup
julianobrasil commentedon Nov 22, 2017
@mmalerba, they are very similar, in fact 😄
This component let me choose the year (it returns an integer instead of a Date or Moment object):
Returning an integer is an alternative when you use a datepicker configured to work in Month or Year mode. It's better than just use a regular input with
type=numberjulianobrasil commentedon Feb 14, 2018
This is a first approach of a wrapper to the datepicker in order to achieve some kind of pickers I need in a project of mine. Maybe it can help if someone needs something like a month picker. It's not fully tested: https://stackblitz.com/edit/angular-mulitdate-picker-demo
@mmalerba, the
yearSelectedandmonthSelectedoutputs really make the picker more flexible. But you know what? In this particular project, I also need a week picker and a semester picker. I worked it around using filters. Is this kind of feature too far from the final goal planned for this component? Month and year pickers are pretty common, but week picker is kind of rare and semester picker... I think I'm the only one in the world who is using it. hahahammalerba commentedon Feb 15, 2018
@julianobrasil I want to try to abstract out the concept of a calendar view, have it be some interface that people can provide their component as. Then allow people to put custom views in the calendar and have the calendar interact with them via that interface. Similar idea to how the form field works with custom form field controls, haven't gotten the time to work on it yet though.
Iznogood1 commentedon Mar 7, 2018
@julino,
thanks a lot fot your month picker
Excatly what I need !
krishragu12395 commentedon Mar 9, 2018
@julianobrasil is that feature is added? . If yes means please elaborate that. Thanks in advance.
julianobrasil commentedon Mar 9, 2018
@krishragu12395, if you are referring to the year picker and month picker, strictly speaking, no, they are not available yet. If you are asking whether it's possible to emulate these features, fortunately, the answer is yes.
However, this demands the 6-beta versions of material to be installed because you'll use the new
@Outputs:yearSelectedandmonthSelected(see the stackblitz demo in my comment above). For now, you can read about them over here as the docs site is likely to be updated just when the team releases the final version.krishragu12395 commentedon Mar 9, 2018
Thank you @julianobrasil ...
30 remaining items
tobiasschweizer commentedon Apr 17, 2020
I had a look at the
date-rangebranch. As @mmalerba has pointed out to me, there is a new component that lets the user select a date range with day precision. Internally, this is represented as aDateRangewith a start and an end date:components/src/material/datepicker/date-selection-model.ts
Lines 12 to 26 in 9f01912
In the template, the start and end dates can be bound to an
inputvia the form control name. So it's like in the case of picking a single date, only that for a range you get two dates.I think from the logical point of view this is all that's needed to represent dates with month and year precision. Internally, these can be treated as ranges from the first to the last day of a month or a year. In the GUI, however, these need to be displayed differently. Clicking on a month or year should be enough to create a date range for a date with month or year precision, the same applies when initializing an existing date range.
I think
DateRangeneeds an additional memberprecisionwhich could beday,month, oryear. Given a range likestart: 04/01/2020, end: 04/30/2020, this information would be displayed differently depending onprecision: either as an actual selection from the first to the last day of the month (day precision) or just as an active month in month view (month precision). The same applies when the user just wants to define a year like 2020 (internally represented as a date range from the first day to last day of the year).I am also thinking of periods, but they can be represented by two date ranges. With imprecise start and end dates of a period you then have four dates, but logically you just have to worry about the start date of the period's beginning end the end date of the period's end.
@mmalerba I am not sure if this belongs here. Shall I try to describe this in a doc? Where would that go?
mmalerba commentedon Apr 21, 2020
@tobiasschweizer we have a design doc template, though I'm not sure all of the sections really apply in this case, since we're talking about adding a feature to an existing component, not making a new component. I think just to give us a starting point to discuss you can copy the above into the "Proposed implementation" section.
Though it might be possible to represent the precision as a
DateRange, I'm not sure if that's the right thing to do here. There are a number of places where we use theDateRangeas a cue to show a little bit different UI. The logic in these areas could get event more confusing if aDateRangeis sometimes not really a range and just a different precision date. I think if we wanted to represent precision like that we would probably create a separate class calledPeriodor something.I think the question of how we represent precision is important, but probably more important is how we work it into the existing pieces that are in play in the datepicker. Some of the pieces are definitely hardcoded to a specific precision (e.g. MonthView), and we might need to come up with a more generic framework that allows for more flexibility in terms of precision
tobiasschweizer commentedon Apr 23, 2020
@mmalerba Ok, thanks for the input. I made a copy of the design doc template and will proceed as you proposed. I will then share the doc with you.
tobiasschweizer commentedon May 5, 2020
@mmalerba I shared the design doc with you that I created. Are you able to access it?
mmalerba commentedon May 5, 2020
Yep, its on my list of things to take a look at
andreElrico commentedon Jan 14, 2021
Here is how I solved it:
https://stackblitz.com/edit/angular-pyeuls?file=app%2Fdatepicker-views-selection-example.html
infacto commentedon Apr 12, 2021
Please remove moment js. This should not be a dependency to achieve things like that. I see that Angular Material officially supports moment js. There is an adapter for the mat-datepicker etc. This is really annoying. Instead of that, allow date processor, a middleware or something like this. ... This framework is too complicated. You should get inspired by other Frameworks like Vuetify etc. And note that there are better alternatives to momentjs. Like dayjs or date-fns, etc. ...
I just wonder: Why does the mat-datepicker input displays the correct local date, but fails on user input with exactly the same date format? And why does this work in other Frameworks? Sorry, I'm pissed a bit because this is very time wasting stuff. ... Argh...
axell9641 commentedon Oct 28, 2021
https://material.angular.io/components/datepicker/examples#datepicker-views-selection
angular-automatic-lock-bot commentedon Aug 12, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.