简体   繁体   中英

Date object from json response

Hi I have a scenario where I need to convert the JSON response to a date object so that I can display it in different format. Here is the JSON response what I am getting:

responseData: [{"id":10,"createdDate":"1 Sep, 2014 12:48:52 PM"}]

In the UI i need display create date as 1 Sep, 2014 or I need to display it in mm-dd-yyyy formate. How can I do this? Do I need to create date object from json response or I should play around with parsing the json reponse? Any help greatly appreciated.

只需将moment.js添加到您的页面即可

moment(responseData[0].createdDate).format("MM-DD-YYYY");

Try this code hope works

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);

在创建json obj后操作日期对象,如下所示 - > new Date(JSON.parse('[{"id":10,"createdDate":"1 Sep, 2014 12:48:52 PM"}]')[0].createdDate)

pass data.createdDate to renderdate function

function renderDate(value) {        
    var getDate;
    if (Date.parse(value)) {
        getDate = new Date(value);
    }

    //used for json date format like /Date(1224043200000)/
    else {
        getDate = new Date(parseInt(value.substr(6)));
    }

    return ((getDate.getMonth() + 1) + "-" + getDate.getDate() + "-" + getDate.getFullYear());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM