I have a json string date value as below:
{[
{
"id": "2044020453",
"startDate": "2022-11-19T04:14:11 07:00",
"endDate": "2022-11-19T04:14:11 07:00",
}
]}
string endDate = JsonConvert.SerializeObject(jo["endDate"], Formatting.None, new
JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
endDate value is "\"2022-11-18T21:14:11Z\""
DateTime endDateTime = DateTime.ParseExact(endDate, "yyyy-MM-
ddTHH:mm:ssZ",System.Globalization.CultureInfo.InvariantCulture);
Always fails to convert to date because there is a backslash in-front and end of the "endDate"
how to clean the backslash?
I have tried:
endDate.Replace("\\", "") --> no luck
Regex.Unescape(endDate) also no luck
anybody can help?
CodePudding user response:
It looks like there is no backslash but a quote character around the string, which is just escaped by the backslash. So actually the string is:
"2022-11-18T21:14:11Z"
then you can trim it:
endDate = endDate.Trim('"');
and then parse it:
DateTime endDateTime = DateTime.Parse(endDate, CultureInfo.InvariantCulture);
CodePudding user response:
It's a double quote that needs to be removed:
endDate.Replace("\"", "")