locked
Problem in JSON Date Format RRS feed

  • Question

  • User-118859203 posted

    Hi guys,

    When I have sent JSON to format date

    by

    $("#StDate").text(formatDate(Date(result.StartDates))); 

    function formatDate(jsDate) {
      var resultDate = dateFormat(jsDate, "mm/dd/yyyy");
      return resultDate;
    }

    I am getting back string as  /Date(1224043200000)/.

    Anyone can help me to fix that. Thanks for your help!

    Friday, December 7, 2018 12:47 AM

Answers

  • User-552477072 posted

    To get from JSON you can use 

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

    Please don't forget to mark as Answer if it helps you! Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2018 1:36 AM
  • User-474980206 posted

    this a data format invented by microsoft, and only used by their json serializer. interestingly their json deserializer does not support this format. the numeric value inside is meant to be passed to the Date method to get a valid javascript date variable (not string).

      var jsDate = Date(/\/Date\((\d+)\)\//.exec(jsonDate)[1])

    note: you can replace the json serializer with json.net, and the data will be an iso date string

        

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2018 8:06 PM

All replies

  • User-552477072 posted

    Just use this 

    var resultDate = new Date(parseInt(resultDate.substr(6)));

    Will work fine. Thanks.

    Please don't forget to mark as Answer if it helps you! Thanks

    Friday, December 7, 2018 1:06 AM
  • User-118859203 posted

    Hi parseInt is consider okay.

    But I don't want to be fixed like substr(6). I want to format  /Date(1224043200000)/ and pull out actual date. thank you

    Friday, December 7, 2018 1:22 AM
  • User-552477072 posted

    To get from JSON you can use 

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

    Please don't forget to mark as Answer if it helps you! Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2018 1:36 AM
  • User-474980206 posted

    this a data format invented by microsoft, and only used by their json serializer. interestingly their json deserializer does not support this format. the numeric value inside is meant to be passed to the Date method to get a valid javascript date variable (not string).

      var jsDate = Date(/\/Date\((\d+)\)\//.exec(jsonDate)[1])

    note: you can replace the json serializer with json.net, and the data will be an iso date string

        

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2018 8:06 PM
  • User-118859203 posted

    Thanks bruce and rubaiyat2009 for your cooperation.

    Monday, December 10, 2018 12:44 AM