HOWTO: Convert DateTime to JSON format
Posted: 4/7/2020 3:33:05 PMBy: PrintableKanjiEmblem
Times Read: 1,728
Likes: 0 Dislikes: 0
Topic: Programming: .NET Framework
JSON has a weird text format. Here's how to easily convert to it from a C# DateTime:
public string GetJSONFromUserDateTime(DateTime dt) { dt = dt.ToUniversalTime();
string jsonDateTime = string.Empty; if (dt != null) { JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
jsonDateTime = JsonConvert.SerializeObject(dt, microsoftDateFormatSettings); jsonDateTime = jsonDateTime.Replace("\"\\/Date(", "").Replace(")\\/\"", ""); } return jsonDateTime; }
Rating: (You must be logged in to vote)