Elixir語言中 NaiveDateTime.add
相關用法介紹如下。
用法:
add(naive_datetime, amount_to_add, unit \\ :second)
(從 1.4.0 開始)
@spec add(Calendar.naive_datetime(), integer(), System.time_unit()) :: t()
將指定的時間量添加到
。NaiveDateTime
接受來自
的任何 System.time_unit/0
unit
中的 amount_to_add
。負值將及時向後移動。
例子
# adds seconds by default
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], 2)
~N[2014-10-02 00:29:12]
# accepts negative offsets
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], -2)
~N[2014-10-02 00:29:08]
# can work with other units
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], 2_000, :millisecond)
~N[2014-10-02 00:29:12]
# keeps the same precision
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10.021], 21, :second)
~N[2014-10-02 00:29:31.021]
# changes below the precision will not be visible
iex> hidden = NaiveDateTime.add(~N[2014-10-02 00:29:10], 21, :millisecond)
iex> hidden.microsecond # ~N[2014-10-02 00:29:10]
{21000, 0}
# from Gregorian seconds
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
~N[2014-10-02 00:29:10]
傳遞
會自動將其轉換為 DateTime
,丟棄時區信息:NaiveDateTime
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> NaiveDateTime.add(dt, 21, :second)
~N[2000-02-29 23:00:28]
相關用法
- Elixir NaiveDateTime.utc_now用法及代碼示例
- Elixir NaiveDateTime.from_gregorian_seconds用法及代碼示例
- Elixir NaiveDateTime.truncate用法及代碼示例
- Elixir NaiveDateTime.from_erl!用法及代碼示例
- Elixir NaiveDateTime.from_iso8601用法及代碼示例
- Elixir NaiveDateTime.from_erl用法及代碼示例
- Elixir NaiveDateTime.convert!用法及代碼示例
- Elixir NaiveDateTime.new!用法及代碼示例
- Elixir NaiveDateTime.to_erl用法及代碼示例
- Elixir NaiveDateTime.diff用法及代碼示例
- Elixir NaiveDateTime.to_iso8601用法及代碼示例
- Elixir NaiveDateTime.local_now用法及代碼示例
- Elixir NaiveDateTime.compare用法及代碼示例
- Elixir NaiveDateTime.to_string用法及代碼示例
- Elixir NaiveDateTime.new用法及代碼示例
- Elixir NaiveDateTime.convert用法及代碼示例
- Elixir NaiveDateTime.to_time用法及代碼示例
- Elixir NaiveDateTime.from_iso8601!用法及代碼示例
- Elixir NaiveDateTime.to_date用法及代碼示例
- Elixir NaiveDateTime.to_gregorian_seconds用法及代碼示例
- Elixir NaiveDateTime用法及代碼示例
- Elixir Node.ping用法及代碼示例
- Elixir Node.start用法及代碼示例
- Elixir StringIO.flush用法及代碼示例
- Elixir Calendar.ISO.date_to_string用法及代碼示例
注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 NaiveDateTime.add(naive_datetime, amount_to_add, unit \\ :second)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。