Wrong time on request body

Hey there! I am using Flatpicker to let users set time on horarioPartida and before calling .save() my object looks like this:

Selection_222

But when I check the request payload the horarioPartida looks like this:

What is setting the wrong time on the database.

Inside my application route I have configured default runtime locale/timeZone:

beforeModel() {
   this.moment.setLocale('pt-br');
   this.moment.setTimeZone('America/Sao_Paulo');
}

Is this expected? What should I do to get the right time zone? Do I have an option to set this while Ember is serializing dates?

Thanks in advance!

I’ll go over everything that’s happening here, but apologies if I’ve overexplaining or if you already know all of this :grinning_face_with_smiling_eyes:

So in the first screenshot horarioPartida shows a time of 12:00:00 GMT-0300 meaning 12:00 local time (Sao Paulo, as you specified) which is GMT minus 3 hours.

If you’re using Ember Data I assume you’re using the date transform in your model e.g.

horarioPartida: attr('date'),

If that’s the case then the transform simply calls <date>.toISOString() which converts a date object to a string in ISO8601 date format, which is what you see in the second screenshot:

2021-03-31T15:00:00.000Z

The Z indicates “Zulu time” which is typically GMT/UTC. Since the local timezone is GMT-3, that means the local time of 12:00:00 is 15:00:00 in GMT timezone, so the times are actually the same. The difference is the representation of the time. ISO8601 is very standard format for representing dates, which is why the date transform uses it, and (at least I think) it’s fairly standard for storing them in databases. So it makes sense that you would be sending that format to the server.

So at the end of the day I think everything is probably working how it should and the date being sent is correct. You could verify this by loading the date from the database back into your ember app, which will run it through the other side of the transform, and (should by default, but may require manual specification) show the date in the browser local time the same as when the date was chosen and then saved.

Wow! Thank you so much for explaining me all of this! That was awesome!

That’s what I was expecting, to moment.js shows time using local time zone. But it is not working at all! After rendering the objects I’ve got this:

Selection_224

I am not sure if it’s showing the wrong time because I am getting the wrong DateTime format from the server or if it is something related to moment.js. The expected time is GMT-3.

I have the following config:

//config/environment.js
 moment: {
      outputFormat: 'L',
      includeTimezone: 'all',
      includeLocales: ['pt-br'],
      allowEmpty: true
    },
//routes/application.js

beforeModel() {
        this.moment.setLocale('pt-br');
        this.moment.setTimeZone('America/Sao_Paulo');
}

Do you deal with dates using another library? Is there a better option than moment.js? :thinking:

My backend uses Flask and Marshmallow to serialize data, I’ve checked and the DateTime field default format is ISO8601. I am going through moment.js documentation to check if there is further information about formatting dates.

Thank you again!

I see what is happening now!

That’s the date that I’m getting from the server:

It doesn’t have the “Z” part, which I think is causing the date to not be parsed correctly.

If I get a date that has a complet format:

It is parsed as expected!

Thank you again @dknutsen !

1 Like