1

Here is a JS function which is populating a html page with JSON data via AJAX. I've got everything work but the format of the JSON data date and time, here is how the JSON data date looks like in a browser:

    /Date(1378916205770)/

And here is my JS function:

  $("input.Btn_load_more").click(function () {
    var valueNum = $("input.index_page").val();
    $.getJSON("http://localhost:64127/DistributorFeed/feeds/36926477759/" + valueNum + "/10", function (obj) {
        $.each(obj, function (key, value) {
            $("<div class=\"feed_item\"><div class=\"Wrapper_feed_item\"><img class=\"feed_icon\" src=\"/Content/imgs/4life_img_holder.png\" alt=\"\"/><p class=\"feed_title\">"
                + value.FeedType +
                "</p><p class=\"feed_message\">" + value.Message + "</p> <p class=\"feed_date\">" + value.DateAdded + "</p> </div>")
                .appendTo(".Wrapper_feed");
        });
    });      
});

I am wondering that how can I format this JSON data to the format like this "Thursday, June 13, 2013 - 7:24:22 AM" ? Any help will be appreciated.

Boaz
  • 19,892
  • 8
  • 62
  • 70
7537247
  • 303
  • 5
  • 19
  • See this question, it should answer yours http://stackoverflow.com/questions/2649057/proper-way-to-format-date-from-database-using-javascript-jquery - I would recommend using the ToString on the date object you are making. In there you can use the same as in C# – Dumpen Sep 13 '13 at 21:09

1 Answers1

1

If you are allowed to use additional libraries, I HIGHLY suggest using Moment.js.

Moment.JS Formatting Options here

This library will make your life much easier for these sorts of tasks. There's a host of formatting available so doing something like you're after you could use

var json_data = '/Date(1378916205770)/';
var asAMoment = moment(json_data);
var formattedString = asAMoment.format('dddd, MMMM Do YYYY, h:mm:ss A');
//formattedString = "Thursday, June 13, 2013 - 7:24:22 AM";
Oliver Kane
  • 888
  • 1
  • 6
  • 23
  • Moment.js is one of my "indispensable" libraries. Unless someone has a very good reason why they can't handle a few k of scripts, Underscore, jQuery, and Moment are a must. – Oliver Kane Sep 17 '13 at 13:19