0

I have a datatable and I export it to JSON format. One column of datatable is a date column, like 2013-01-09 02:18:11.117.

When I export it, it seems like "Date":"\/Date(1357690691117)\/"

My code is below:

DataTable dataTable = GetData();

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dataTable.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(seperator, columnNames));

foreach (DataRow row in dataTable.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(seperator, fields));
}

return sb.ToString();

Why does the date column seem different?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
UserStackk
  • 75
  • 1
  • 2
  • 7

2 Answers2

2

See the Stack Overflow question Format a Microsoft JSON date?:

var date = new Date(parseInt(jsonDate.substr(6)));

Stack Overflow question The "right" JSON date format is more specific and direct to your question.

Community
  • 1
  • 1
Jones
  • 1,480
  • 19
  • 34
  • @RoyiNamir: Ok, please enlighten me. How would you put "Sets the default radix, in the range 2 to 16, to the value of expression." to use here? Not pulling your leg, I really can't fathom it, and you got an upvote, so I must be missing something obvious. – Marjan Venema Dec 24 '13 at 14:07
  • @MarjanVenema IE8 : http://i.stack.imgur.com/FpNsr.png – Royi Namir Dec 24 '13 at 14:14
0

It's auto-conversion: 2013-01-09 02:18:11.117 = "/Date(1357690691117)/" (milliseconds)

For example (conversion to "dd/mm/yyyy" format):

function formatJsonDate(jsonDate) {
    return (new Date(parseInt(jsonDate.substr(6)))).format("dd/mm/yyyy");
};

var test = formatJsonDate('/Date(1357690691117)/');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2700840
  • 457
  • 3
  • 13