0

I want to pull a date from db and display it in a telerik ddl. I am able to pull the date and display it, but cant get the formatting to work. The date is display like this:

/Date(13496724000000)/

Here is my Controller action to pull the date from db:

    [HttpPost]
    public ActionResult Date(int id)
    {
        var dates = (from p in db.vwdb.Where(u => u.Id == id)
                           group p by p.Date into g
                    select g.Key).ToList();

        return Json(new { data = Convert.ToDateTime(weeks.Min())});
    }

Here is my jquery to display it in ddl:

function populateDate() {
    var link = '/Users/Date';
    var dataSource = [];
    $.ajax({
        type: 'POST',
        url: link,
        data: { id: id },
        dataType: 'json',
        success: function (result) {
                var dateOf = result.data;
                dataSource.push({ Text: dateOf , Value: dateOf });
            $("#Date").data("tDropDownList").dataBind(dataSource);
        },
        error: function (result) {
            alert(result.message);
        }
    });
};

I want to display the date in this format: "MM/DD/YYYY" instead of "/Date(13496724000000)/"

Any help is much appreciated.

3
  • and where do you use dates list? Commented Oct 29, 2012 at 20:56
  • @HamletHakobyan use it to display dates on a web page. Commented Oct 29, 2012 at 21:08
  • Is moment.js something for you? Commented Oct 30, 2012 at 12:35

2 Answers 2

2

You can Parse this json Date format and convert it into "MM/DD/YYYY" format.

Try:

// jsonDate = "/Date(13496724000000)/"

function parseJsonDateTime(jsonDate) {

    var dateString = jsonDate.substr(6);
    var currentTime = new Date(parseInt(dateString));
    var month = currentTime.getMonth();
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();

   var date = month + "/" + day + "/" + year;
    return date;

};
Sign up to request clarification or add additional context in comments.

Comments

1

Try like this:

success: function (result) {
    var date = new Date(parseInt(result.data.replace("/Date(", "").replace(")/",""), 10));
    var dateOf = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
    dataSource.push({ Text: dateOf , Value: dateOf });
    $("#Date").data("tDropDownList").dataBind(dataSource);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.