Sending date from angular frontend to aspnetcore backend - timezone troubles

535 Views Asked by At

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?

1

There are 1 best solutions below

2
Code Name Jack On

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?