前端时间戳格式化

huangapple go评论68阅读模式
英文:

Formatting timestamps on the frontend

问题

以下是翻译好的部分:

"如同许多其他问题中所提到的,我该如何让 Django 管理面板使用我浏览器的当前时区?

通常解决这个问题的方法是让服务器猜测客户端想要的时区(可能将时区偏好存储在数据库中等),然后相应地提供服务。然而,我从未看到建议使用客户端的 JavaScript 来转换时间戳。

在我看来,这似乎是完美的解决方案,因为 JavaScript 无需猜测,而服务器也无需关心客户端的视觉偏好。鉴于这不是默认的方法,我认为我可能忽略了一些重大缺陷。

为什么在像 Django 管理面板这样的应用中通常不使用 JavaScript 来转换时间戳?"

英文:

As has been asked in many other questions, how can I make e.g. Django admin portal use my browser's current time zone?

The usual way to solve that is to get the server to guess the time zone the client wants (potentially storing time zone preferences in the database etc.) and serve accordingly. However, nowhere do I see suggestions to use client-side JavaScript to convert timestamps.

To me it looks like the perfect solution, as JavaScript doesn't need to guess, and the server doesn't need to concern itself with effectively visual preferences of clients. Given that this is not the default approach, I assume there's some huge flaw I'm missing.

Why is JavaScript not typically used to convert timestamps in applications like Django admin portal?

答案1

得分: 1

这个答案不特定于Django或任何使用的技术。

正如在许多其他问题中所问的那样,我如何让例如Django管理门户使用我的浏览器当前的时区?

一般来说,如果您想要在后端处理时间戳(意味着:创建格式化输出),您必须以某种方式将当前时区传递给后端。如果在初始请求中没有办法将该信息传递给后端,您必须以后端可以访问的方式存储该信息,通常意味着将信息存储在数据库中(但不一定,也可以以其他方式存储)。

解决这个问题的通常方法是让服务器猜测客户端想要的时区(可能在数据库中存储时区首选项等),然后相应地提供服务。

是的,完全正确(除非您知道首选项时,这不是猜测)。

然而,我从未见过建议使用客户端JavaScript来转换时间戳的建议。

对我来说,这看起来像完美的解决方案,因为JavaScript不需要猜测,服务器也不需要关心客户端的视觉首选项。鉴于这不是默认方法,我认为我可能忽略了一些巨大的缺陷。

我理解为“让JavaScript决定如何格式化时间戳而不传递特定的格式规则”。

我想到的是,您不一定在通常使用的国家/时区的计算机上使用计算机。想象一下,您坐在美国的计算机前,而您居住在德国。在这种情况下,您更愿意看到根据_德国_的格式规则格式化的日期,而不是根据美国的格式规则。即使这是您自己的计算机(也许您只是度假,谁知道?),在国外时,您很可能会更改时钟(和时区),仍然希望看到日期按照您习惯的方式格式化。这意味着:

new Date(TIMESTAMP).toLocaleString()

不起作用,因为它使用主机的区域设置。相反,您必须传递更多的格式信息:

new Date(TIMESTAMP).toLocaleString(LOCALE)

那个LOCALE信息从哪里来?您必须在某个地方存储它。

这归结为:无论您使用哪种技术来格式化时间戳,您仍然必须存储用户的_首选项_(除非您不关心它们 - 但那么整个问题都是无意义的)。这意味着无论您是在前端还是后端进行格式化,您都不需要“猜测”,同样也不能让主机“决定”,否则您很可能做错了什么。

为什么像Django管理门户这样的应用程序通常不使用JavaScript来转换时间戳?

因为Django是使用Python运行的。

英文:

This answer is not specific to Django or any technology used.


> As has been asked in many other questions, how can I make e.g. Django admin portal use my browser's current time zone?

In general, you have to pass the current timezone to the backend in some way if you want to process timestamps (meaning: create formatted output) in the backend. If there's no way for you to pass that information to the backend inside the initial request, you have to store the information in a way the backend can access it, which usually means to store the infos in a database (but not necessarily, it could also be stored otherwise).

> The usual way to solve that is to get the server to guess the time zone the client wants (potentially storing time zone preferences in the database etc.) and serve accordingly.

Yes, exactly (with the exception that it's not guessing when you know the preferences).

> However, nowhere do I see suggestions to use client-side JavaScript to convert timestamps.
>
> To me it looks like the perfect solution, as JavaScript doesn't need to guess, and the server doesn't need to concern itself with effectively visual preferences of clients. Given that this is not the default approach, I assume there's some huge flaw I'm missing.

I understand that as "let JavaScript decide how to format the timestamp without passing in specific formatting rules".

What comes to mind is that you don't necessarily use a computer in the country/timezone you usually use it in. Imagine you sit in front of a computer in the US while you live in Germany. In that case, you'd prefer to see dates formatted according to german formatting rules and not US formatting rules. Even if it's your own computer (maybe you are just on vacation, who knows?), it is likely that you change the clock (and timezone) while being abroad and still want to see dates being formatted the way you are used to. Meaning that:

new Date(TIMESTAMP).toLocaleString()

does not work, because it uses the host's locale. Instead, you'd have to pass in further formatting information:

new Date(TIMESTAMP).toLocaleString(LOCALE)

Where does that LOCALE information come from? You have to store it somewhere.

This boils down to: Regardless of which technology you use to format timestamps, you still have to have the user's preferences stored (except if you do not care about them - but then this whole question is nonsense). This means it does not matter if you format on the front- or backend, you do not have to "guess" and you likewise cannot let the host "decide" or you very likely did something wrong.

> Why is JavaScript not typically used to convert timestamps in applications like Django admin portal?

Because Django runs with Python.

huangapple
  • 本文由 发表于 2023年7月17日 19:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定