如何将 updated_at 转换为我所在时区的传统12小时格式?

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

How to Convert updated_at to Conventional 12 Hour Format with My Time Zone?

问题

在Phoenix中,Ecto给了我一个名为updated_at的时间戳,它的显示格式如下:

2023-05-31T18:44:44

我想将其格式化为12小时制,并转换为我的时区。

我可以通过以下方式将其格式化为12小时制,同时不影响时区:

Calendar.strftime(updated_at, "%d-%m-%y %I:%M:%S %p")

这将给我一个格式如下的时间:

31-05-23 06:59:01 PM

这正是我想要的,但时区仍然不正确。

我不需要将带有我的时区的时间存储到数据库中,我只需要将现有的时间数据以我描述的方式呈现到GUI中。

我可以解析字符串,将相关部分转换为整数,进行数学运算,然后重新转为字符串,但我觉得可能有一个更简单的答案。

我的时区比记录在数据库中的时间少5小时。

英文:

In Phoenix, Ecto gives me a timestamp named updated_at, and it presents time in this way:

2023-05-31T18:44:44

I want to format it to 12 hour time and convert it to my time zone.

I can format it to a 12-hour time style while not affecting the time zone by doing this:

Calendar.strftime(updated_at,"%d-%m-%y %I:%M:%S %p")

This gives me a format that looks like this:

31-05-23 06:59:01 PM

This is what I want, but the time zone is still incorrect.

I don't need to store the time with my time zone to the database, I just need to render the preexisting time data to the GUI formatted the way I described.

I could parse the string, turn the relevant parts into integers, do the math and re-stringify it, but I figure there might be a simpler answer.

My time zone is 5 hours less than the time recorded to the database.

答案1

得分: 0

第一,与其将日期时间存储为字符串,最好将其存储为数据库中的日期时间,并通过在模式中使用timestamps(type: :utc_datetime_usec)(或:utc_datetime) 配置ecto以将其返回为DateTime

但是,假设您只有字符串2023-05-31T18:44:44,您可以执行以下操作:

  1. 使用NaiveDateTime.from_iso8601!/2将字符串转换为NaiveDateTime
  2. 使用DateTime.from_naive/3NaiveDateTime转换为DateTime,指定数据存储的时区。
  3. 使用DateTime.shift_zone/3DateTime转换为您的时区。
  4. 使用Calendar.strftime/3DateTime进行格式化,就像您已经在做的那样。

您需要安装并配置一个时区数据库

例如,假设使用Europe/LondonAmerica/New_York时区(请注意,结果可能会有所不同,但对于此日期,结果为5小时):

"2023-05-31T18:44:44"
|> NaiveDateTime.from_iso8601!()
|> DateTime.from_naive!("Europe/London")
|> DateTime.shift_zone!("America/New_York")
|> Calendar.strftime("%d-%m-%y %I:%M:%S %p")

结果:

"31-05-23 01:44:44 PM"

如果已经配置Ecto以在UTC中提供DateTime,则可以跳过前两个步骤(但是在该日期,纽约实际上比UTC晚4小时,因此结果会有所不同)。

英文:

<!-- language-all: lang-elixir -->

First, instead of a string, it would be better to store the datetime in your database as a datetime, and configure ecto to return it as a DateTime by using timestamps(type: :utc_datetime_usec) (or :utc_datetime) in your schema.

However, assuming you just have the string 2023-05-31T18:44:44, you can do this:

  1. Convert your string to a NaiveDateTime using NaiveDateTime.from_iso8601!/2
  2. Convert the NaiveDateTime to a DateTime, specifying the time zone the data is stored in using DateTime.from_naive/3
  3. Convert the DateTime to your time zone using DateTime.shift_zone/3
  4. Format the DateTime as you are already doing with Calendar.strftime/3

You need to have installed and configured a time zone database.

Example, assuming Europe/London and America/New_York time zones (note the result can vary, but it works out to 5 hours for this date):

&quot;2023-05-31T18:44:44&quot;
|&gt; NaiveDateTime.from_iso8601!()
|&gt; DateTime.from_naive!(&quot;Europe/London&quot;)
|&gt; DateTime.shift_zone!(&quot;America/New_York&quot;)
|&gt; Calendar.strftime(&quot;%d-%m-%y %I:%M:%S %p&quot;)

Result:

&quot;31-05-23 01:44:44 PM&quot;

If you have configured Ecto to give you DateTimes in UTC, you can skip the first two steps (but New York is actually 4 hours behind UTC at that date, so the result will differ).

huangapple
  • 本文由 发表于 2023年6月1日 04:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377060.html
匿名

发表评论

匿名网友

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

确定