C# Json.NET WCF Rest DateTime Format

梦想与她 提交于 2019-12-07 18:58:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!