开发者

How do I convert a json date format to the following dd/mm/yyyy

开发者_运维问答I've seen other examples but I am looking for this specific format if possible?.


Here is a quick format from JSON date format to required date format using jQuery:

For a JSON date string like:

/Date(1339439400000)/

try:

var newFormattedDate = $.datepicker.formatDate('mm/dd/yy', new Date(Date(your_JSON_date_string)));

and it will result in date like this: 09/14/2012


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

    var testJsonDate = formatJsonDate('/Date(1224043200000)/');

    alert(testJsonDate);


There is no "json date format". json only returns strings.

Date javascript object can parse a variety of formats. See the documentation.

You can do:

var myDate = new Date(myDateAsString);


There are a few suggestions in the answers to this post -- How do I format a Microsoft JSON date?

If those examples aren't working, you can just format it manually. Here's a quick fiddle that demonstrates it -- http://jsfiddle.net/dhoerster/KqyDv/

$(document).ready(function() {
    //set up my JSON-formatted string...
    var myDate = new Date(2011,2,9);
    var myObj = { "theDate" : myDate };
    var myDateJson = JSON.stringify(myObj);

    //parse the JSON-formatted string to an object
    var myNewObj = JSON.parse(myDateJson);

    //get the date, create a new Date object, and manually format the date string
    var myNewDate = new Date(myNewObj.theDate);
    alert(myNewDate.getDate() + "/" + (myNewDate.getMonth()+1) + "/" + myNewDate.getFullYear());
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜