components icon indicating copy to clipboard operation
components copied to clipboard

Day incorrect in datepicker

Open aceleghin opened this issue 9 years ago • 46 comments

Bug, feature request, or proposal:

When I select a date I see the correct date in the field but, when I save, the datepicker send the day before the date I have selected

here's the code: <md-form-field> <input mdInput [(ngModel)]="fareCalendar.startDate" name="startDate" [mdDatepicker]="picker" placeholder="startDate"> <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle> <md-datepicker #picker></md-datepicker> </md-form-field>

I am using angular 4 and @angular/material": "^2.0.0-beta.10 rtcnm

but the date: f5z10

How can I send the UTC date

aceleghin avatar Sep 19 '17 10:09 aceleghin

Could you try to reproduce it here, please: https://plnkr.co/edit/8nRKSJ?p=preview ?

julianobrasil avatar Sep 19 '17 11:09 julianobrasil

I just want that the date I have selected in calendar is already in UTC timezone

aceleghin avatar Sep 19 '17 12:09 aceleghin

That's why I'm asking you to reproduce it in the plunk. For me it's working fine, but it's possible that some bug is happening in your case:

image

If it's just a matter of configs, stackovervlow.com is more suitable than this channel.

julianobrasil avatar Sep 19 '17 12:09 julianobrasil

The calendar returns a javascript Date object. What you've shown in above message seems like something generated from other source (it looks like a JSON.stringify() result or maybe from a database service). To get back the Javascript Date object, you have to do (really not sure how did you get that result):

fareCalendar.startDate = new Date(fareCalendar.startDate);

julianobrasil avatar Sep 19 '17 12:09 julianobrasil

If I select th date: 6/9/2017 and print it I see: Wed Sep 06 2017 00:00:00 GMT+0200

Then I save the date in a body const body = { startDate: this.fareCalendar.startDate }; And make post request. I am using Http client

aceleghin avatar Sep 19 '17 15:09 aceleghin

Oh, you may be refering to the fact that the UTC time add/subtract your current time offset. Look at the picture below: I'm in a GMT+2h00 zone, so, my UTC value is offset by 2 hours. So, if I set the hours to 23h00 in my Date object, my UTC date get offset to the day after (as in 1h00 am).

image

julianobrasil avatar Sep 19 '17 15:09 julianobrasil

Yes, I want the date selected is already UTC, I used primeng calendar and there is a property like utc="true" that solve this. If I select 22/9/2017 I want send: 2017-09-22T00:00:00.000Z

aceleghin avatar Sep 19 '17 15:09 aceleghin

Well... I really don't know if it's planned to support such a utc attribute.

cc: @mmalerba

julianobrasil avatar Sep 19 '17 16:09 julianobrasil

We do plan to add support for different timezones/offsets, but its not there yet. In the mean time you could make a custom DateAdapter that works with UTC dates to accomplish this.

Here is the DateAdapter class you would have to extend: https://github.com/angular/material2/blob/master/src/lib/core/datetime/date-adapter.ts

Some examples of existing implementations: https://github.com/angular/material2/blob/master/src/lib/core/datetime/native-date-adapter.ts https://github.com/angular/material2/blob/master/src/material-moment-adapter/adapter/moment-date-adapter.ts

And some documentation that may help: https://material.angular.io/components/datepicker/overview https://blog.angular.io/taking-advantage-of-the-angular-material-datepicker-237e80fa14b3

mmalerba avatar Sep 19 '17 16:09 mmalerba

@lippomano I have the same request. Did you get any solution about it ? thank you for any suggestions.

we125182 avatar Sep 21 '17 05:09 we125182

@we125182 I write a method that manipulate every date it is not the best practice but it's simpler for me

aceleghin avatar Sep 28 '17 07:09 aceleghin

I'm also getting a incorrect date.

bug-on-datetime

adaojunior avatar Oct 03 '17 14:10 adaojunior

I get this exact same behaviour on https://material.angular.io/components/datepicker/overview It always adds +1 day after selected.

adaojunior avatar Oct 03 '17 14:10 adaojunior

@adaojunior if you're seeing the same issue on the docs site its likely some issue related to your locale / time zone. Any additional information you can provide would be useful. If you're using Moment.js in your app (or willing to try it) you could give the MomentDateAdapter a try and see if it resolves your issue. Just add the MatMomentDateModule from @angular/material-moment-adapter at your application root instead of MatNativeDateModule

mmalerba avatar Oct 03 '17 16:10 mmalerba

@mmalerba same issue on the docs, see: bug-on-angular-datepicker

I'm using MdNativeDateModule on my application.

adaojunior avatar Oct 03 '17 16:10 adaojunior

@mmalerba Hello, is it possible to import MatMomentDateModule from @angular/material-moment-adapter when using beta12? I cannot find the way to do it, it's seems to me this module is not yet released?

npen avatar Oct 11 '17 14:10 npen

@npen yeah I think it missed the release, you can install it from here in the mean time: https://github.com/angular/material2-moment-adapter-builds

mmalerba avatar Oct 11 '17 16:10 mmalerba

Adding +1 day is a new "feature" with the day picker whilst using the default DateAdapter. I believe it has been introduced in beta 12.

This also happens for me on the material angular site: https://material.angular.io/components/datepicker/overview

Today is the 12th of October at 22:30. To replicate:

  • Open the datepicker the 11 October is "circled" as today's date.
  • Select the 12th, the 13th is displayed when the picker is closed.
  • When I reopen it, the 12th is selected

I'm using an Aussie time zone. 22:30. When I switch back to Europe, it works as intended despite being on the same day as in Australia.

jt-helsinki avatar Oct 12 '17 11:10 jt-helsinki

@jt-helsinki Thanks for the detailed info, strangely I am unable to reproduce it using the same settings. I tried 12th of October 2017 at 22:30 in the following time zones (all of the Australian ones listed on my system): Adelaide, Brisbane, Broken Hill, Currie, Darwin, Ecula, Hobart, Lindeman, Lord Howe, Melbourne, Perth, Sydney.

All of them showed the 12th circled and selected the 12th after clicking (on docs site) date

If anyone knows what I'm doing wrong please let me know...

mmalerba avatar Oct 12 '17 16:10 mmalerba

Looks like you're doing exactly what I was doing.

I notice I'm on a different OS. An unlikely shot, could it be a OS/browser thing?

I'm on: OSX Chrome (Version 61.0.3163.100 (Official Build) (64-bit))

Anyone else care to confirm?

The strange thing with this is that the date had not yet flipped to the following day. Europe and Australia were still on the same day.

jt-helsinki avatar Oct 12 '17 19:10 jt-helsinki