Invalid date using <date>

I use datepicker to select a date and send it to the server.

When I register a JS value, I get the correct result:

Tue Mar 22 2016 00:00:00 GMT+0100 (Mitteleuropรคische Zeit) 

but in ajax request this is

 2016-03-21T23:00:00.000Z 

I do not change the value, just passing the object to the corner http function.

Is Angular required for any configuration?

+9
javascript angularjs datepicker angular-material
source share
3 answers

These two lines represent the same time. One of them is in UTC, i.e. GMT +0, which you can see at the end of Z The other is in a different time zone, in particular GMT +1 hour.

If you had javascript date objects for both strings and turned them into integers, that is, seconds elapsed from January 1, 1970 UTC, you will find them the same. They represent the same moment, but at two different geographical points.

 var d1 = new Date('Tue Mar 22 2016 00:00:00 GMT+0100'); var d2 = new Date('2016-03-21T23:00:00.000Z'); Number(d1); // 1458601200000 Number(d2); // 1458601200000 

This is usually good. Working in time zones is becoming very confusing. I believe that best of all, the server will always work in UTC.

+5
source share

You can try the following code snippet

 dateObj.setMinutes((dateObj.getMinutes() + dateObj.getTimezoneOffset())); 

There is no need for localization, use this code immediately before making any service call. It will give you the exact date you selected in the date index.

It will work in all time zones (+) and (-),

Example: 2016-03-21 00:00:00 GMT+0100 , the above code is 2016-03-21 01:00:00 GMT+0000 as its 2016-03-21 01:00:00 GMT+0000 . While on the Service it transforms it as 2016-03-21 00:00:00 .

I think this will solve your problem.

+7
source share

https://github.com/angular/material/pull/9410

Check version 1.1.1+. This will solve your problem.

 <md-datepicker ng-model="date" ng-model-options="{ timezone: 'utc' }"></md-datepicker> 
+4
source share

All Articles