Reputation: 47
I'm doing web app using Angular and WebAPI. I have a strange problem with difference dates between Angular Controller and WebAPI controller. On my html page i have a input for type of date:
<input id="signupDateofBirth" ng-model="dateofBirth" placeholder="Date of birthday" type="date" max="1998-01-01" min="1900-12-31" class="form-control">
Let's assume that the date is 31.12.1990. I'm assigning it in my angular controller and while debugging date is correct: 31.12.1990 :
DateOfBirth: $scope.dateofBirth
DateOfBirth: Mon Dec 31 1990 00:00:00 GMT+01
The problem appears when i get object in WebAPI controller. The date is the day before to the date of Angular Controller. It's 30.12.1990 like on image:
Any ideas? Maybe I need to format it?
Upvotes: 0
Views: 55
Reputation: 44
This appears because of the timezone.
In your controller, date is correctly formatted with your local timezone (GMT+1).
When your API receive it, it doesn't know how to handle it: you have to specify that the dates returned in your JSON are in local timezone.
Try to specify your local timezone in your API startup.cs, e.g. with newtonsoft JSON :
json.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
EDIT
You should always pass an ISO formatted date to your API and vice versa. See : https://wikipedia.org/wiki/ISO_8601
Upvotes: 1