JS格式化JSON后的日期

最近在做微信小程序 写了个WCF服务,返回实体中有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)\')

  

posted @ 2017-06-07 08:54  亮有一姬  阅读(2486)  评论(0编辑  收藏  举报