Elixir語言中 DateTime.to_iso8601
相關用法介紹如下。
用法:
to_iso8601(datetime, format \\ :extended, offset \\ nil)
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) ::
String.t()
將給定的日期時間轉換為 ISO 8601:2019 格式。
默認情況下,
返回格式為 "extended" 格式的日期時間,以方便人類閱讀。它還通過傳遞DateTime.to_iso8601/2
:basic
選項支持"basic" 格式。
僅支持轉換 ISO 日曆中的日期時間,嘗試從其他日曆轉換日期時間將引發。您還可以選擇為格式化字符串指定偏移量。
警告:ISO 8601 日期時間格式不包含時區或其縮寫,這意味著轉換為此類格式時會丟失信息。
例子
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 3600, std_offset: 0, time_zone: "Europe/Warsaw"}
iex> DateTime.to_iso8601(dt)
"2000-02-29T23:00:07+01:00"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "UTC",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
iex> DateTime.to_iso8601(dt)
"2000-02-29T23:00:07Z"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended)
"2000-02-29T23:00:07-04:00"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :basic)
"20000229T230007-0400"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 3600)
"2000-03-01T04:00:07+01:00"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07+00:00"
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07Z"
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
相關用法
- Elixir DateTime.to_unix用法及代碼示例
- Elixir DateTime.to_date用法及代碼示例
- Elixir DateTime.to_gregorian_seconds用法及代碼示例
- Elixir DateTime.to_naive用法及代碼示例
- Elixir DateTime.to_string用法及代碼示例
- Elixir DateTime.to_time用法及代碼示例
- Elixir DateTime.truncate用法及代碼示例
- Elixir DateTime.now用法及代碼示例
- Elixir DateTime.from_naive用法及代碼示例
- Elixir DateTime.from_unix用法及代碼示例
- Elixir DateTime.from_unix!用法及代碼示例
- Elixir DateTime.convert用法及代碼示例
- Elixir DateTime.new!用法及代碼示例
- Elixir DateTime.utc_now用法及代碼示例
- Elixir DateTime.add用法及代碼示例
- Elixir DateTime.diff用法及代碼示例
- Elixir DateTime.convert!用法及代碼示例
- Elixir DateTime.from_naive!用法及代碼示例
- Elixir DateTime.now!用法及代碼示例
- Elixir DateTime.from_gregorian_seconds用法及代碼示例
- Elixir DateTime.shift_zone!用法及代碼示例
- Elixir DateTime.from_iso8601用法及代碼示例
- Elixir DateTime.new用法及代碼示例
- Elixir DateTime.compare用法及代碼示例
- Elixir DateTime.shift_zone用法及代碼示例
注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 DateTime.to_iso8601(datetime, format \\ :extended, offset \\ nil)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。