DateTime Field is wrong on initial load

Hi. I'm using the datetime field for filtering in a SQL query. My issue is that, on initial load, the value from the box is coming in as local time, but when I click on the field and click back out, the value updates to be UTC, which is what I want. How do I set the value so that it is always in UTC?
before clicking datetime field
image
after clicking datetime field
image

Use moment - the library is already supplied in Retool so using moment() will be easy

1 Like

Hi @justi,

To set the datetime field value to always be in UTC, you can use the moment.utc() function. The moment.utc() function converts a datetime value to UTC.

To use the moment.utc() function, you can add the following code to your Retool query:

{{ moment.utc(datetime_field_name).format('YYYY-MM-DDTHH:mm:ssZ') }}

This code will convert the datetime value to UTC and format it in the ISO 8601 format.

For example, the following query uses the moment.utc() function to set the datetime field value to UTC:

select * from table where datetime_field >= {{ moment.utc(datetime_field_name).format('YYYY-MM-DDTHH:mm:ssZ') }}

This query will select all rows where the datetime field is greater than or equal to the current datetime in UTC.

You can also use the moment.utc() function to set the default value for a datetime field. To do this, you can add the following code to the Default value property of the datetime field:

{{ moment.utc().format('YYYY-MM-DDTHH:mm:ssZ') }}

This code will set the default value for the datetime field to the current datetime in UTC.

By using the moment.utc() function, you can ensure that datetime field values are always in UTC, regardless of the user's local time.

Hope this helps.

:grinning:

Patrick

4 Likes

That did the trick. thank you!

2 Likes