2017-03-11 189 views
1

我有一個日期。使之成爲該格式的通過時間我如何格式化:使用Timex格式化日期時間

Wed, 02 Oct 2002 15:00:00 +0200 

Wed, 02 Oct 2002 15:00:00 GMT 

Wed, 02 Oct 2002 15:00:00 EST 

我已經試過這樣:

Timex.format!(my_date, "%D, %d %M %Y %H:%i:%s T", :strftime)) 

但它拋出了一個例外:

%Timex.Format.FormatError{message: {:format, "Expected end of input at line 1, column 16"}} (expected a string) 

雖然它正在轉換爲其他更簡單的格式,沒有錯誤。

+0

這格式字符串中有多處錯誤。 '%D'不是星期幾,時區是'%Z'(或'%z'或其他,但不是'T')。請參閱https://hexdocs.pm/timex/Timex.Format.DateTime.Formatters.Strftime.html。 – Dogbert

回答

2

我相信你正在尋找RFC1123 DateTime格式:

iex(1)> Timex.now |> Timex.format!("{RFC1123}") 
"Sat, 11 Mar 2017 12:04:21 +0000" 
iex(2)> Timex.now |> Timex.to_datetime("America/Chicago") |> Timex.format!("{RFC1123}") 
"Sat, 11 Mar 2017 06:04:50 -0600" 
+0

是,對於RSS 2.0 – Kuqa

+0

http://stackoverflow.com/questions/9405309/how-to-properly-place-date-in-pubdate-element-on-rss-feed – Kuqa

+0

你知道爲什麼'「一% ,%d%b%Y%H:%M:%S%z「'給我」格式字符串無效,必須包含至少一個指令。「? – Kuqa