Dor Cohen
Dor Cohen

Reputation: 17080

How to return json date from C# DateTime

I'm using Json.NET/newtonsoft and I have the following C# class:

public class EntityDefinition
{
    [DataMember]
    public string CreatedBy { get; set; }
    [DataMember]
    [JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]
    public DateTime CreatedOn { get; set; }
}

When I try to return this class in my wcf I'm getting the following JSON:

{
  "GetDefinitionResult": {
    "CreatedBy": "Dor",
    "CreatedOn": "/Date(1466428742000+0300)/"
   }
}

How can I get the date to be parsed without the "Date(", meaning only the milliseconds or in iso format "yyy-mm-dd"

I tried using the JsonProperty convertor but it still returns the "Date()"

[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]

Upvotes: 1

Views: 4288

Answers (2)

Niro
Niro

Reputation: 394

WCF is using DataContractSerializer by default to serialize/deserialize messages and the mentioned date format is its default format.

If you'd like to change the way your WCF service serialize/deserialize messages, you should replace some things in the service's behavior (mainly IDispatchMessageFormatter). However, it's too long to describe here and there's a great tutorial about it here

Good Luck

Upvotes: 1

redsam
redsam

Reputation: 145

Try [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter)) or use CustomDateConverter as explained here in Parsing JSON DateTime from Newtonsoft's JSON Serializer

Upvotes: 1

Related Questions