Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Results 'as of time' is not shown in user's timezone #9

Open
markojerkic opened this issue Sep 18, 2022 · 0 comments
Open

Results 'as of time' is not shown in user's timezone #9

markojerkic opened this issue Sep 18, 2022 · 0 comments

Comments

@markojerkic
Copy link

The way this currently works is that you take the current DateTime at the time of the request, like so:
https://github.com/TheoBr/faster-round/blob/a13b9a0ca4c4a76ba761f060d3d6326fb6a6c3ff/src/pages/results.astro#L7
And this is how it is rendered:
https://github.com/TheoBr/faster-round/blob/a13b9a0ca4c4a76ba761f060d3d6326fb6a6c3ff/src/pages/results.astro#L63-L64
But this bit of js is also executed on the server so it prints the local DateTime as if the user is in the same timezone as the server on which the code is executing.
This results in the following being rendered:

As you can see, the rendered time is off by two hours. I'm located in UTC+2 timezone, while the server is either running at UTC+0 timezone, or the actual closest edge (assuming this is rendered on the edge) is in that timezone.

Fix

I am not quite sure how you would solve this in an Astro component.
You could perhaps do something like this:

<script define:vars={{ createdAt }}>
  document.getElementById("some-id").value = new Date(createdAt).toLocaleString();
</script>

I am unaware of any easier way of changing the value of a DOM element in an Astro component, which does not seem elegant nor safe.

Reactive component?

Another way to solve this issue would be to port this component into a Preact component. But this might be overkill as it is only a single variable which is changed only at the first load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant