英文:
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
,您可以执行以下操作:
- 使用
NaiveDateTime.from_iso8601!/2
将字符串转换为NaiveDateTime
。 - 使用
DateTime.from_naive/3
将NaiveDateTime
转换为DateTime
,指定数据存储的时区。 - 使用
DateTime.shift_zone/3
将DateTime
转换为您的时区。 - 使用
Calendar.strftime/3
对DateTime
进行格式化,就像您已经在做的那样。
您需要安装并配置一个时区数据库。
例如,假设使用Europe/London
和America/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:
- Convert your string to a
NaiveDateTime
usingNaiveDateTime.from_iso8601!/2
- Convert the
NaiveDateTime
to aDateTime
, specifying the time zone the data is stored in usingDateTime.from_naive/3
- Convert the
DateTime
to your time zone usingDateTime.shift_zone/3
- Format the
DateTime
as you are already doing withCalendar.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):
"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")
Result:
"31-05-23 01:44:44 PM"
If you have configured Ecto to give you DateTime
s 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论