Jsonconverter Web api datetime property get 01/01/0001

I have a web api controller method that accepts a composite object in which one of the properties is DateTime. The client wants us to display the date in yyyyMMdd format. i use for property

[JsonConverter(typeof(CustomDateConverter))]
public DateTime TransactionDate { get; set; }

public class CustomDateConverter : IsoDateTimeConverter
{
    public CustomDateConverter()
    {
        DateTimeFormat = "yyyyMMdd";
    }
}

      

And there is another property, whether the client will only send the time in hhmmss format When I send the date in a fiddler to test this method, I get the date value in the model as 01/01/0001 00: 00: 000

+3


source to share


1 answer


You are using the code for a console app, see fiddle

For web APIs, you might want to register your format CustomDateConverter

in global.asax



JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new CustomDateConverter());
GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);

      

0


source







All Articles