2016-07-27 152 views
1

我返回CalculationDate从我的WebApi控制器方法的DateTime类型。从c#返回日期时间仅返回日期

return response = new GetResponse 
         { 
          CalculationDate = DateTime.Parse(
           "2016-07-19T21:18:47.473Z", 
           CultureInfo.InvariantCulture),        
         }; 

,当我从小提琴手测试我收到响应如下

{ 
    "success": true, 
    "content": { 
    "calculationDate": "2016-07-19", // without time and milliseconds part.  
    } 
} 

我应该怎么返回的日期时间,这样我会收到日期和时间用毫秒来吗?

+0

什么是CalculationDate – Neil

+0

尝试使用以下内容:CalculationDate = DateTime.Parse( “2016 -07-19T21:18:47.473Z“.ToString(”MM/dd/yyyy HH:mm:ss.fff“, CultureInfo.InvariantCulture)) –

+0

@Neil - 它是DateTime类型 –

回答

1

以下设置在我的Global.asax中是问题所在。

var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
    jsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd"; 

我不得不将其更改为以下

var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
jsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ"; 

@RubenAguilar - 谢谢你的指针