为什么这个DateTime.ParseExact调用失败了?

时间:2016-05-02 17:27:11

标签: c# parsing datetime

var dateValue = "Mon, 02 May 2016 12:00 PM EDT";
var date = DateTime.ParseExact(
   dateValue,
   "ddd, dd MMM yyyy hh:mm tt K",
   System.Globalization.CultureInfo.InvariantCulture);

尽我所知,从the official format string documentation开始,这应该有效。相反,它会使用相当无用的消息System.FormatException

来引发String was not recognized as a valid DateTime.

有没有办法弄清楚出了什么问题?

3 个答案:

答案 0 :(得分:1)

K Custom Format Specifier不接受时区字符串。

如果您可以提供小时偏移而不是字符串,则可以使用"z"

var dateValue = "Mon, 02 May 2016 12:00 PM -4";
var date = DateTime.ParseExact(
   dateValue,
   "ddd, dd MMM yyyy hh:mm tt z",
   System.Globalization.CultureInfo.InvariantCulture);

答案 1 :(得分:0)

这应该有效

import Ecto.Query

def login_by_email(conn, email, _opts) do
  user = 
    MyApp.User
    |> join(:inner, [u], e in assoc(u, :emails))
    |> where([u, e], e.value == ^email)
    |> MyApp.Repo.one!()    

  conn
  |> assign(:current_user, user)
  |> put_session(:user_id, user.id)
  |> configure_session(renew: true)

end

答案 2 :(得分:0)

https://msdn.microsoft.com/en-us/library/shx7s921%28v=vs.110%29.aspx指定DateTime.Kind枚举有3个成员。所以也许它不喜欢你指定" EDT"作为一种。

 Member name    Description
 Local          The time represented is local time.
 Unspecified    The time represented is not specified as either local time or Coordinated Universal Time (UTC).
 Utc            The time represented is UTC.