I have a class with two DateTime properties. I need to serialize each of the properties with a different format. How can I do it? I tried:
JsonConvert.Serial
I realise this is an old question, but I stumbled upon it during my search for same question.
Newtonsoft has now a DateFormatString property in JsonSerializerSettings class which you can use. I came to this question looking for answer and have just found the property, I have used it as below and it works as below:
private const string _StrDateFormat = "yyyy-MM-dd HH:mm:ss";
private static string GetJSON(object value)
{
return JsonConvert.SerializeObject(value, new JsonSerializerSettings
{
DateFormatString = _StrDateFormat
});
}
When value will have a DateTime object, it will convert it into string respecting _StrDateFormat string.
Perhaps this official link can be updated?
Regards.