question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date offsets with Angular6/AspNetCore2 WebApi combo

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
RicoSutercommented, May 29, 2018

Ok, i found the bug / it’s a bug - stop trying to solve it 😃

0reactions
nstohlercommented, May 30, 2018

Thanks!

It seems to be working now, I’m still testing it all. Will let you know if something comes up

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.tseries.offsets.DateOffset
Each offset specify a set of dates that conform to the DateOffset. For example, Bday defines this set to be the set of...
Read more >
Date offsets — pandas 2.0.3 documentation
DateOffset. Standard kind of date increment used for a date range. ... Describes monthly dates like "the Tuesday of the 2nd week of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found