Created
June 2, 2017 10:45
-
-
Save hidegh/d5747136a9fe3673aaad1ef9b04a226c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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