Js格式化Json返回的DateTime类型的日期数据
深山老妖浏览:7532019-01-03 16:19:41本文累计收益:0我也要赚钱

ajax调用后台接口时,如有DateTime类型的列会变成 /Date(1494524134000+0800)\ 这种格式 不能正常显示,但也不能为了这个吧所有服务的DateTime字段都改成String类型 于是找了一个JS的扩展方法来格式化日期

function ChangeDateFormat(jsondate) {
    jsondate = jsondate.replace("/Date(", "").replace(")/", "");
    if (jsondate.indexOf("+") > 0) {
        jsondate = jsondate.substring(0, jsondate.indexOf("+"));
    }
    else if (jsondate.indexOf("-") > 0) {
        jsondate = jsondate.substring(0, jsondate.indexOf("-"));
    }

    var date = new Date(parseInt(jsondate, 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

    return date.getFullYear()
        + "年"
        + month
        + "月"
        + currentDate
        + "日"
        + " "
        + date.getHours()
        + ":"
        + date.getMinutes();
}
 
//调用方法:ChangeDateFormat('/Date(1494524134000+0800)\')
评论列表
发表评论
+ 关注