简体   繁体   中英

JSON DateTime serialisation in c# handle both single digit and double digit hour

I have a 3rd party application that send JSON DateTime in both single and double digit Hour format. The hour is sent as single digit when less than 10 and double digit when greater than or equal to 10.

eg

2020-12-17T 9 :00:00.000-04:00

2020-12-17T 11 :00:00.000-04:00

I expect the 2020-12-17T9:00:00.000-04:00 sent as 2020-12-17T 09 :00:00.000-04:00.

Is there any DateFormatString that can handle both single and double digital hour?

Currently, I am using the default settings in Newtonsoft and it throws exception in parsing the single digit string to DateTime.

I think I should not ask the question at the beginning. It obviously the 3rd party company does not pass the right format.

For those who want to get the date time string in this format "2020-12-17T11:00:00.000-04:00", the format is "yyyy-MM-ddThh:mm:ss.fffzzz"

var dateString = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss.fffzzz");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM