Date offsets with Angular6/AspNetCore2 WebApi combo
See original GitHub issueI have an Angular 6 app that talks to a Asp.Net Core 2 WebApi backend. I produce the ng clients using nswag (msbuild version) from the Asp.Net Core 2 server project.
POST: I created a simple form, where one of the inputs is a simple text input where users can specify a date in the format YYYY-MM-DD (just for simplicity) in a reactive form component. The date gets parsed by momentjs and then sent (along with other data) to the backend. In the case of post / [FromBody], I had the problem that the moment date got converted and had an offset of 2 hours (My locale is de-CH) when it reached the server. I’m only interested in the date, not the time part, so I constructed my date like this:
var d = '2018-5-28'; // from form
var today = moment(d).startOf('day');
console.log(now.toISOString()); // what gets sent to webapi: 2018-05-27T22:00:00.000Z
This results in the WebApi action getting the wrong date. When I do this before posting, I get the correct date on the server:
var d = '2018-5-28'; // from form
var now = moment(d).utc().startOf('day');
console.log(now.toISOString()); // what gets sent to webapi: 2018-05-28T00:00:00.000Z
GET: Now I remembered I had some GET actions that take a date as a route parameter, so I tried to send the second date (utc’d) there:
GET
http://localhost:7733/api/test-date-get/2018-05-28T00:00:00.000Z
In the action, I just bounced back the received date object:
{
"date": "2018-05-28T02:00:00+02:00",
"year": 2018,
"month": 5,
"day": 28
}
What I want is to not care about any time offsets at all. I store (mostly) dates without any time in the database. Is there a clean way to achieve this? Should I use some other NSwag options to create the typescript code (I thought it’s nice to have moment objects instead of javascript dates)? Or is there something I should configure in Asp.Net Core 2 / Angular / MomentJS to have them agree better on the date formats?
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (8 by maintainers)
Ok, i found the bug / it’s a bug - stop trying to solve it 😃
Thanks!
It seems to be working now, I’m still testing it all. Will let you know if something comes up