Problem passing Angular time to C# Core

Asked

Viewed 217 times

0

The problem is to pass an hour value through an observable method (Angular 8) to a controller (C# Core 2.2), at the time of sending the data is correct:

inserir a descrição da imagem aqui

However, when I receive the values, it is automatically added +3 hours:

inserir a descrição da imagem aqui

I’m currently running locally (localhost), what is the best approach to solving the Timezone problem globally? Thanks in advance!

  • Probably the problem is that the browser and its server are in different timezones

  • Yes, how do I set up . net core p/ do not add up +3 hours globally? To always pick the time provided.

  • I honestly do not understand why some users vote negative for the question, what is the problem with the question? Instead of just judging...

  • I was able to add one more parameter in the conversion: link.data_link = new Date(this.datePipe.Transform(data_link, "yyyy-MM-dd HH:mm:ss", "GMT-6")), is that correct? Should I proceed this way for all the dates I will forward? This does not seem consistent to me...

2 answers

1


Working with dates is always a challenge, so it’s essential that someone dictates the rule on which Timezone to use. The normal is if the UTC (which would be the time/time zone "zero") as default, pq from theVoce only needs to apply the user’s time zone.

If you need to force this behavior in javascript Voce can use the getUTCDate. But if you need to treat this server-side Voce you can use the Touniversaltime()

This way everyone talks in the same time zone, and Oce only needs to treat the presentation so that it is presented in the correct way.

0

From the image shown, there is no error. Part of DateTime is the time zone, which in Brazil is always GMT-3 (since there is no longer daylight saving time). The time shown in Angular indicates that it is in GMT. There are several ways to treat the time zone on the front end, the easiest one should be with the use of libraries like the Moment.js.

Looking at the backend image, the time will have the "normal" component of time and also the time zone. The ideal is to always work with UTC, avoiding this kind of problem and convert the time only in the presentation layer, taking into account the user’s time zone.


Side note: UTC and GMT is not the same thing "philosophically" speaking, but in the background will have the same result. So the "GMT" you’re seeing at Angular would be equivalent to UTC at DateTime.UtcNow no. Net.

  • 1

    Brazil is GMT -3

  • I need the back end to receive exactly the time I am passing, regardless of the time zone, because the user is setting this time to exercise certain activity. What should I set up in the back end so that this conversion doesn’t happen?

  • @Marcelovismari, thank you. Corrected.

  • @Alessandro the error is not in the backend, but in the front end. It is a risk to assume that all customers will send the date with the same time zone. You should see the components that are used on the front to make sure that you add the client’s time zone on the date that is passed to your api. Otherwise, you will not be able to do the converão without gambiarras, since you assume that the last time is in another time zone, you would have to see the datetime offset server and multiply by 2, to then add or subtract from the received date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.