简体   繁体   English

POST和GET方法的DateTime格式不同

[英]DateTime format is different for the POST and GET methods

I have a JSON string. 我有一个JSON字符串。 When I send the string from my view to my controller via the POST method, one of the properties of this JSON, which is a datetime, is mapped in the format "dd/mm/yyyy". 当我通过POST方法将字符串从视图发送到控制器时,此JSON的属性之一(即日期时间)以“ dd / mm / yyyy”格式映射。 But when I send the same JSON string by the GET method, the same property is mapped in the format "mm/dd/yyyy", leaving the property with NULL for dates with days greater than 12. How to fix this problem? 但是,当我通过GET方法发送相同的JSON字符串时,相同的属性将以“ mm / dd / yyyy”格式映射,而对于大于12天的日期,该属性保留为NULL。如何解决此问题? Can this be a Visual Studio problem? 这可能是Visual Studio的问题吗? I'm using the Brazilian date format. 我正在使用巴西日期格式。

The response is always in format specified by RFC 7231 . 响应始终采用RFC 7231指定的格式。 See for example this link . 例如参见此链接

you need to Specific Culture on the get request. 您需要对特定文化的要求。 since the browser use different culture than your sever , they should match, 由于浏览器使用的文化不同于您的服务器,因此它们应该匹配,

  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"));

for more check this : https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx 有关更多信息,请参见: https : //msdn.microsoft.com/zh-cn/library/5hh873ya(v=vs.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM