Skip to content

Month and/or year selecion in DatePicker #4853

Closed

Description

@julianobrasil

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

  1. fxck commented on May 29, 2017

    @fxck
    Contributor

    it will be added eventually #675 (comment) (and following discussion)

  2. added
    feature
    on May 30, 2017
  3. self-assigned this
    on May 30, 2017
  4. mmalerba commented on Nov 21, 2017

    @mmalerba
    Contributor

    dup of #5845

  5. mmalerba commented on Nov 21, 2017

    @mmalerba
    Contributor

    oh whoops, misread this, not a dup

  6. added
    P3
    on Nov 21, 2017
  7. julianobrasil commented on Nov 22, 2017

    @julianobrasil
    ContributorAuthor

    @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):

    image

    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=number

  8. julianobrasil commented on Feb 14, 2018

    @julianobrasil
    ContributorAuthor

    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 yearSelected and monthSelected outputs 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. hahaha

  9. mmalerba commented on Feb 15, 2018

    @mmalerba
    Contributor

    @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.

  10. Iznogood1 commented on Mar 7, 2018

    @Iznogood1

    @julino,

    thanks a lot fot your month picker
    Excatly what I need !

  11. krishragu12395 commented on Mar 9, 2018

    @krishragu12395

    @julianobrasil is that feature is added? . If yes means please elaborate that. Thanks in advance.

  12. julianobrasil commented on Mar 9, 2018

    @julianobrasil
    ContributorAuthor

    @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: yearSelected and monthSelected (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.

  13. krishragu12395 commented on Mar 9, 2018

    @krishragu12395

    Thank you @julianobrasil ...

  14. 30 remaining items

  15. tobiasschweizer commented on Apr 17, 2020

    @tobiasschweizer
    Contributor

    I had a look at the date-range branch. 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 a DateRange with a start and an end date:

    /** A class representing a range of dates. */
    export class DateRange<D> {
    /**
    * Ensures that objects with a `start` and `end` property can't be assigned to a variable that
    * expects a `DateRange`
    */
    // tslint:disable-next-line:no-unused-variable
    private _disableStructuralEquivalency: never;
    constructor(
    /** The start date of the range. */
    readonly start: D | null,
    /** The end date of the range. */
    readonly end: D | null) {}
    }

    In the template, the start and end dates can be bound to an input via 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 DateRange needs an additional member precision which could be day, month, or year. Given a range like start: 04/01/2020, end: 04/30/2020, this information would be displayed differently depending on precision: 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?

  16. mmalerba commented on Apr 21, 2020

    @mmalerba
    Contributor

    @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 the DateRange as a cue to show a little bit different UI. The logic in these areas could get event more confusing if a DateRange is 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 called Period or 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

  17. tobiasschweizer commented on Apr 23, 2020

    @tobiasschweizer
    Contributor

    @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.

  18. tobiasschweizer commented on May 5, 2020

    @tobiasschweizer
    Contributor

    @mmalerba I shared the design doc with you that I created. Are you able to access it?

  19. mmalerba commented on May 5, 2020

    @mmalerba
    Contributor

    Yep, its on my list of things to take a look at

  20. andreElrico commented on Jan 14, 2021

    @andreElrico
  21. infacto commented on Apr 12, 2021

    @infacto

    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...

  22. axell9641 commented on Oct 28, 2021

    @axell9641
  23. angular-automatic-lock-bot commented on Aug 12, 2025

    @angular-automatic-lock-bot

    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.

  24. locked and limited conversation to collaborators on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: material/datepickerfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions