Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching data from JSON After serialization .. I am getting all data properly but for Data I am getting dates in "YYYY-MM-DD". I want a date in "MM-DD-YYYY". How to convert date formant while fetching data below is the code how I fetching data.
I am adding data in list kv (key value pair)

What I have tried:

if (!string.IsNullOrWhiteSpace(Json1["dateOfBirth"].ToString()))
                        {
                            kv.Add("Date of Birth",Json1["dateOfBirth"].ToString());
                           
                        }
Posted
Updated 13-Jul-22 22:42pm

If you really do need to change the format of the date, parse it into a DateTime format (or Date format if you're using the right version of .NET), and then format the parsed version. The question I would ask, though, is do you really need to change the format? So much time is invested in doing this in backend code when, the majority of the time, a formatted version is just required for the user interface. If that's the case, you shouldn't try to force the format to a particular string; you should use the user locale preferences instead.
 
Share this answer
 
You are either using a string value in your JSON, or your ToString needs a format parameter to specify exactly what you want.

So start with the debugger to find out exactly what datatype is in your JSON data as loaded.

The later is simple: Formatting a DateTime for display - format string description[^]

The former is more of a problem, as it means you need to parse the data into a DateTime, and then format it - and that means checking for valid values and potentially handling multiple input formats correctly as well as "empty" values.

I'd suggest that you are possibly handling the JSON data badly: an array implies that you aren't reading the JSON into strongly typed classes so all data has the same type (probably string) and that means you may need to manually process a lot of information to make it actually usable and be sure it's valid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900