问题
Take a look at the following example code, I would like the output to be a WCF date format "/Date(1237951967000)/" or the time zone variant.
class Program
{
public class Test
{
public DateTime Date { get; set; }
}
static void Main(string[] args)
{
var test = new Test
{
Date = DateTime.Now
};
var json = JsonConvert.SerializeObject(test);
Console.WriteLine(json);
}
}
Here is the output:
{"Date":"2013-05-09T11:17:38.7990259-07:00"}
How can I adjust the above code to give the desired format?
{"Date":"\/Date(1237951967000)\/"}
回答1:
var settings = new JsonSerializerSettings() {DateFormatHandling= DateFormatHandling.MicrosoftDateFormat};
var json = JsonConvert.SerializeObject(test, settings);
来源:https://stackoverflow.com/questions/16468704/c-sharp-json-net-wcf-rest-datetime-format