0

I have a problem sending dates via a NSwag generated typescript client to an aspnetcore webapi.

The generated code is:

getExchangeRate(from: string, to: string, date: Date): Observable<ExchangeRate> {
    let url_ = this.baseUrl + "/api/ExchangeRate/GetExchangeRate/{from}/{to}/{date}";
    if (from === undefined || from === null)
        throw new Error("The parameter 'from' must be defined.");
    url_ = url_.replace("{from}", encodeURIComponent("" + from));
    if (to === undefined || to === null)
        throw new Error("The parameter 'to' must be defined.");
    url_ = url_.replace("{to}", encodeURIComponent("" + to));
    if (date === undefined || date === null)
        throw new Error("The parameter 'date' must be defined.");
    url_ = url_.replace("{date}", encodeURIComponent(date ? "" + date.toISOString() : "null"));
    url_ = url_.replace(/[?&]$/, "");

.. the values at the time of sending are

enter image description here

.. on my backend I receive this

enter image description here

I am really tying myself in knots getting my head around this.

The date dropdown in the Angular app was set to 5th Apr 2023 and is in BST. The ISO standard string format means that is -1hr so it sends 4th Apr 2023 23:00

I understand that is correct but what I actually want is to find the exchange rate on the given day, not on the UTC equiv of the day. I don't really know the origin of the request - it could be one of several timezones.

Preferably of course I don't want to change the NSwag generation as the client is regenerated regularly, but it is possible if necessary.

What I really want is just to send the date that they see on the screen - I don't care about the times at all and would be happy to drop time and timezone information.

Any ideas?

Dominic Shaw
  • 210
  • 2
  • 10

1 Answers1

0

The problem is that date.toISOString() will convert it to UTC and discard the timezone information.

You can look at these answers to know how to preserve timezone information.

How to ISO 8601 format a Date with Timezone Offset in JavaScript?

Code Name Jack
  • 2,856
  • 24
  • 40
  • Thanks for the quick reply. Using your idea I can format it and send it as 20230405T00:00:00.000Z however I will have to do this upstream of every single date submission and I can't always control that within the NSwag generated client. Is there anything I can do to just make it all date, no time or timezone? – Dominic Shaw Apr 11 '23 at 12:56
  • Dotnet has Date only type but last I worked on it, Serialization was not happening properly. If that is fixed in dotnet 7 maybe tht can help you – Code Name Jack Apr 11 '23 at 15:06