components icon indicating copy to clipboard operation
components copied to clipboard

[datepicker] time is reset to midnight when field is blurred

Open tchandelle opened this issue 8 years ago • 5 comments

Bug

Create a mat-datepicker, with MatMomentDateModule adapter and with this provider

  providers: [{
    provide: MAT_DATE_FORMATS,
    useValue: {
      parse: {
        dateInput: 'L LTS',
      },
      display: {
        dateInput: 'L LTS',
        monthYearLabel: 'MMM YYYY',
        dateA11yLabel: 'L LTS',
        monthYearA11yLabel: 'MMMM YYYY',
      },
    }
  }]

Select a date

The input display 05/28/2018 12:00:00 AM

Edit the time

What is the expected behavior?

The date should change the time

What is the current behavior?

The time is reset to 12:00:00 AM when user blur the field

What are the steps to reproduce?

StackBlitz doesn't work with moment, this is another issue (#9780). But for reference : https://stackblitz.com/edit/angular-material2-issue-25p4rg

What is the use-case or motivation for changing an existing behavior?

It use to work before a62cdb6

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Chrome Version 66.0.3359.181 moment 2.22.1

Angular CLI: 6.0.5
Node: 8.11.1
OS: darwin x64
Angular: 6.0.3
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.0
@angular-devkit/build-angular      0.6.0
@angular-devkit/build-optimizer    0.6.0
@angular-devkit/core               0.6.0
@angular-devkit/schematics         0.6.5
@angular/cdk                       6.1.0
@angular/cli                       6.0.5
@angular/material                  6.1.0
@angular/material-moment-adapter   6.1.0
@ngtools/webpack                   6.0.0
@schematics/angular                0.6.5
@schematics/update                 0.6.5
rxjs                               6.2.0
typescript                         2.7.2
webpack                            4.6.0

tchandelle avatar May 28 '18 13:05 tchandelle

i have the same situation Mozilla Firefox 60.0.1 export const MY_DATE_FORMAT = { parse : { dateInput: 'DD/MM/YYYY HH:mm', }, display: { dateInput: 'DD/MM/YYYY HH:mm', monthYearLabel: 'MM/YYYY', } };

providers : [ { provide : DateAdapter, useClass: MomentDateAdapter, deps : [MAT_DATE_LOCALE] }, { provide : MAT_DATE_FORMATS, useValue: MY_DATE_FORMAT } ]

"@angular/cdk": "^6.1.0", "@angular/animations": "^6.0.3", "@angular/common": "^6.0.3", "@angular/compiler": "^6.0.3", "@angular/core": "^6.0.3", "@angular/forms": "^6.0.3", "@angular/material": "^6.1.0", "moment": "^2.22.1", "moment-timezone": "^0.5.17", "typescript": "2.7.2", "@angular/material-moment-adapter": "^6.1.0",

In Angular material 5.2.4 this didn´t happend.

ing-andresavalos avatar May 28 '18 18:05 ing-andresavalos

This is probably because the DateAdapter doesn't officially support dealing with the time component of the date yet. My guess is that creating a custom DateAdapter that extends the adapter you're currently using and overrides the compareDate method to consider time would fix it: https://github.com/angular/material2/blob/master/src/lib/core/datetime/date-adapter.ts#L241

In case you're not familiar with the DateAdapter, I wrote a blog post about it a while back: https://blog.angular.io/taking-advantage-of-the-angular-material-datepicker-237e80fa14b3

I think your custom one would look like this:

class MyNativeDateAdapter extends NativeDateAdapter {
  compareDate(first: Date, second: Date): number {
    return first.getTime() - second.getTime();
  }
}

Eventually the datepicker will officially support time and we'll probably add a separate method compareDatetime.

Edit: corrected the Medium link

mmalerba avatar May 31 '18 21:05 mmalerba

Thanks. I confirm that extending MomentDateAdapter with the method below solve the issue

compareDate(first: Moment, second: Moment): number {
   return first.valueOf() - second.valueOf();
 }

tchandelle avatar Jun 01 '18 07:06 tchandelle

It doesn't anymore

PunkHaz4rd avatar Jun 18 '19 09:06 PunkHaz4rd

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.