You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using angular 4 and @angular/material": "^2.0.0-beta.10
but the date:
How can I send the UTC date
Reacted by drenda, Pedro Castro Mellado, Maxime Ménard, René Winkler, Puria Rad Jahanbani, Tejashree Deshmukh, Anton Nogin, Kamil Cegliński, Evan Gillogley, AndrewMilko and 12 more
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):
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).
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
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.
@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
Reacted by Simon Bobrov, Diego Kogan, Jamie Gross, Urs, Abhith Rajan, Mykola Riabchenko, Viktor-Bredihin, André Luís, Richard Dunn, Matthias Müller and 1 more
Reacted by Simon Bobrov, Diego Kogan, Viktor-Bredihin, André Luís and Richard Dunn
@raibait, in fact, it doesn't care about it. But the underlying objects that currently store the date values usually do (regular javascript Date objects and Moment.js are the most common cases). This is the reason for so many issues related to wrong dates. Building a date adapter and configuring it correctly is important to avoid headaches.
Maybe the problem here is the fact that the datepicker's value is a Date in the first place. Date is a perfectly good(ish) class for representing an absolute instant in real time; it's a terrible representation of a calendar date. This entire thread, dozens of posts, dozens of users complaining, could have been avoided if the default date adapter went to/from ISO 8601 date-only strings. Every date library (and of course the native Date object) understands them, they are exactly as specific as the UI that generated them, and they can be "upgraded" to an instant or range of real time by simply adding "Z" on the end. What I mean to say is, adding context to go from "2018/11/26" to "2018/11/26Z" makes sense, but going the other way -- trying to remove time-zone-ness from the data -- does not.
Of course, I can fix all this by writing my own date adapter, or going out and getting the Moment-based one, but as long as the default behavior is Date-based, you're going to keep seeing people showing up in this thread.
Reacted by Laurens Van de Water, Matt Gilbert, Viktor-Bredihin, Richard Dunn, Christopher, Marcin Feleniak, Jeferson Duwe and Pascal Müller
I made my self a workaround by overriding the MomentJS DateAdapter compiling the date from UTC and then converting it into the locale date:
import { Inject, Injectable, Optional } from '@angular/core';
import { MAT_DATE_LOCALE } from '@angular/material';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { Moment } from 'moment';
import * as moment from 'moment';
@Injectable()
export class MomentUtcDateAdapter extends MomentDateAdapter {
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
super(dateLocale);
}
createDate(year: number, month: number, date: number): Moment {
// Moment.js will create an invalid date if any of the components are out of bounds, but we
// explicitly check each case so we can throw more descriptive errors.
if (month < 0 || month > 11) {
throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
}
if (date < 1) {
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
}
let result = moment.utc({ year, month, date }).locale(this.locale);
// If the result isn't valid, the date must have been out of bounds for this month.
if (!result.isValid()) {
throw Error(`Invalid date "${date}" for month with index "${month}".`);
}
return result;
}
}
The important line being:
let result = moment.utc({ year, month, date }).locale(this.locale);
@Silthus the adapter you did works like a charm, but I have a requirement to set the format for the date.
How can I achieve that alongside timezone adapter?
I am wondering if adding the useUTC portion to a component's providers instead of the module's providers will not work?
When using the Honolulu timezone (spoofing the browser), we were not able to make moment work, we ended up having to manually remove the timezone offset in order to get the mat-datepicker to choose the correct date:
let newDate= new Date(initialInfo.value); newDate.setMinutes(newDate.getMinutes() + newDate.getTimezoneOffset()); variableValueCtrl.reset(newDate);
When I tried to get it to work with moment it cause a whole host of other issues. However this still causes the selection to be wrong:
Not looking for a solution, just alerting to the issues
Activity
julianobrasil commentedon Sep 19, 2017
Could you try to reproduce it here, please: https://plnkr.co/edit/8nRKSJ?p=preview ?
aceleghin commentedon Sep 19, 2017
I just want that the date I have selected in calendar is already in UTC timezone
julianobrasil commentedon Sep 19, 2017
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:
If it's just a matter of configs, stackovervlow.com is more suitable than this channel.
julianobrasil commentedon Sep 19, 2017
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):
aceleghin commentedon Sep 19, 2017
If I select th date: 6/9/2017 and print it I see:
Wed Sep 06 2017 00:00:00 GMT+0200Then I save the date in a body
const body = { startDate: this.fareCalendar.startDate };And make post request.
I am using Http client
julianobrasil commentedon Sep 19, 2017
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).
aceleghin commentedon Sep 19, 2017
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.000Zjulianobrasil commentedon Sep 19, 2017
Well... I really don't know if it's planned to support such a utc attribute.
cc: @mmalerba
mmalerba commentedon Sep 19, 2017
We do plan to add support for different timezones/offsets, but its not there yet. In the mean time you could make a custom
DateAdapterthat works with UTC dates to accomplish this.Here is the
DateAdapterclass 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
we125182 commentedon Sep 21, 2017
@lippomano I have the same request. Did you get any solution about it ? thank you for any suggestions.
aceleghin commentedon Sep 28, 2017
@we125182 I write a method that manipulate every date it is not the best practice but it's simpler for me
adaojunior commentedon Oct 3, 2017
I'm also getting a incorrect date.
adaojunior commentedon Oct 3, 2017
I get this exact same behaviour on https://material.angular.io/components/datepicker/overview
It always adds +1 day after selected.
mmalerba commentedon Oct 3, 2017
@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
MomentDateAdaptera try and see if it resolves your issue. Just add theMatMomentDateModulefrom@angular/material-moment-adapterat your application root instead ofMatNativeDateModule23 remaining items
Silthus commentedon May 15, 2018
@mmalerba I can send a PR later this day.
fix(dateadapter): moment dates not in utc format
feat(moment-dateadapter): add option to create utc dates
docs(moment-dateadapter): add docs for moment adapter options
glucena commentedon Jul 3, 2018
You can change the default behaviour to parse dates as UTC by providing the MAT_MOMENT_DATA_ADAPTER_OPTIONS and setting it to useUtc: true.
@NgModule({
imports: [MatDatepickerModule, MatMomentDateModule],
providers: [
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
]
})
Check here for more details
smnbbrv commentedon Aug 22, 2018
MAT_MOMENT_DATE_ADAPTER_OPTIONS- oh man, that's so awesome!However, this..., it's not should, it must be default configuration for datepicker.
raibait commentedon Oct 1, 2018
Why does datepicker care about the timezones at all? Who would ever need that?
julianobrasil commentedon Oct 1, 2018
@raibait, in fact, it doesn't care about it. But the underlying objects that currently store the date values usually do (regular javascript Date objects and Moment.js are the most common cases). This is the reason for so many issues related to wrong dates. Building a date adapter and configuring it correctly is important to avoid headaches.
thw0rted commentedon Nov 26, 2018
Maybe the problem here is the fact that the datepicker's
valueis a Date in the first place.Dateis a perfectly good(ish) class for representing an absolute instant in real time; it's a terrible representation of a calendar date. This entire thread, dozens of posts, dozens of users complaining, could have been avoided if the default date adapter went to/from ISO 8601 date-only strings. Every date library (and of course the native Date object) understands them, they are exactly as specific as the UI that generated them, and they can be "upgraded" to an instant or range of real time by simply adding "Z" on the end. What I mean to say is, adding context to go from "2018/11/26" to "2018/11/26Z" makes sense, but going the other way -- trying to remove time-zone-ness from the data -- does not.Of course, I can fix all this by writing my own date adapter, or going out and getting the Moment-based one, but as long as the default behavior is Date-based, you're going to keep seeing people showing up in this thread.
nugrohotanjo commentedon Apr 20, 2019
thanks, it's work very well :)
Achyuthkodali commentedon Jul 15, 2019
@Silthus the adapter you did works like a charm, but I have a requirement to set the format for the date.
How can I achieve that alongside timezone adapter?
my date format: "DD MMM YYYY" // 15 jul 2019
christrude commentedon Sep 9, 2019
I am wondering if adding the
useUTCportion to a component's providers instead of the module's providers will not work?When using the Honolulu timezone (spoofing the browser), we were not able to make moment work, we ended up having to manually remove the timezone offset in order to get the mat-datepicker to choose the correct date:
let newDate= new Date(initialInfo.value); newDate.setMinutes(newDate.getMinutes() + newDate.getTimezoneOffset()); variableValueCtrl.reset(newDate);When I tried to get it to work with moment it cause a whole host of other issues. However this still causes the selection to be wrong:
Not looking for a solution, just alerting to the issues
christrude commentedon Sep 9, 2019
Stackblitz showing crazy go nuts wonky-ness when you have your browser spoofed to India time:
https://stackblitz.com/edit/angular-trude-datetest
And in that stack blitz, look at what it selects in the UI:
So set to the 6th, moment converts to the 5th, mat-datepicker displays the 4th, but selects the 3rd.
angular-automatic-lock-bot commentedon Oct 10, 2019
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.