AKumar
AKumar

Reputation: 1

how to convert Json date format into datetime format(mm/dd/yyyy))?

My jquery function returning date in json format,so i want to know how to convert this into datetime formate "mm/dd/yyyy"

Upvotes: 0

Views: 5379

Answers (3)

Adeel
Adeel

Reputation: 19228

DateTime date1;
DateTime.TryParseExact(formCollection["date"], "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out date1);
  1. it will not throw any exception.
  2. if there is wrong format, you can compare with DateTime.MinValue, that is it succesfully converted.

Upvotes: 2

Guffa
Guffa

Reputation: 700342

There is no date data type in JSON, so what you have is a date formatted as a string. Use the ParseExact method to parse the string into a DateTime value.

Then you can use the ToString method or String.Format method using the pattern MM'/'dd'/'yyyy to format the DateTime value into a string.

Upvotes: 0

ashish.chotalia
ashish.chotalia

Reputation: 3746

How do I format a Microsoft JSON date?

You can use this to get a date from json:

var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));

and then you can use JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.

Upvotes: 0

Related Questions