Skip to content

Instantly share code, notes, and snippets.

@hidegh
Created June 2, 2017 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidegh/d5747136a9fe3673aaad1ef9b04a226c to your computer and use it in GitHub Desktop.
Save hidegh/d5747136a9fe3673aaad1ef9b04a226c to your computer and use it in GitHub Desktop.
/// <summary>
/// Usage: [JsonConverter(typeof(JsonDateTimeConverter), "MM\\/dd\\/yyyy")].
/// Dates created/set in JS had time-zones appended. By using string, this issue won't persist...
/// </summary>
public class JsonDateTimeConverter : DateTimeConverterBase
{
private string CustomFormat { get; }
public JsonDateTimeConverter(string customFormat)
{
CustomFormat = customFormat;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return DateTime.ParseExact(reader.Value.ToString(), CustomFormat, CultureInfo.InvariantCulture);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((DateTime)value).ToString(CustomFormat));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment