Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Json.NET WCF Rest DateTime Format

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)\/"}
like image 789
aelstonjones Avatar asked Jan 27 '26 13:01

aelstonjones


1 Answers

var settings = new JsonSerializerSettings() {DateFormatHandling= DateFormatHandling.MicrosoftDateFormat};
var json = JsonConvert.SerializeObject(test, settings);
like image 158
I4V Avatar answered Jan 29 '26 03:01

I4V