【问题标题】:DateTime format is different for the POST and GET methodsPOST 和 GET 方法的 DateTime 格式不同
【发布时间】:2017-05-08 20:03:59
【问题描述】:

我有一个 JSON 字符串。当我通过 POST 方法将字符串从视图发送到控制器时,此 JSON 的属性之一(日期时间)以“dd/mm/yyyy”格式映射。但是当我通过 GET 方法发送相同的 JSON 字符串时,相同的属性以“mm/dd/yyyy”格式映射,对于大于 12 天的日期,该属性为 NULL。如何解决此问题?这可能是 Visual Studio 的问题吗?我使用的是巴西日期格式。

【问题讨论】:

标签: c# json datetime post get


【解决方案1】:

响应始终采用RFC 7231 指定的格式。参见例如this link

【讨论】:

    【解决方案2】:

    您需要在获取请求时指定特定文化。由于浏览器使用与您的服务器不同的文化,它们应该匹配,

      DateTime dt = DateTime.Now;
      // Sets the CurrentCulture property to U.S. English or whatever your browser using .
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Displays dt, formatted using the ShortDatePattern
      // and the CurrentThread.CurrentCulture.
      Console.WriteLine(dt.ToString("d"));
    

    更多信息请查看:https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 2020-06-11
      • 2015-05-14
      • 2011-10-30
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多