JS~json日期格式化

起因

对于从C#返回的日期字段,当进行JSON序列化后,在前台JS里显示的并不是真正的日期,这让我们感觉很不爽,我们不可能为了这东西,把所有日期字段都变成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(data[i].arrDate)

调用

  $.ajax({
            type: "Get",
            textType: "json",
            url: "/UserInfo/GetUserWithdraw",
            data: { id: id },
            success: function (data) {
                var result = html.replace(reg, function (node, key) {
                    return {
                        'Money': data.Money,
                        'AddTime': ChangeDateFormat(data.AddTime),
                        'CashTime': data.CashTime
                    }[key];
                });

                TsingdaTips.ask({ msg: result, show_btn: false, title: "提现申请详情" });//预计打款时间等于申请时音后的(5号或20号)
            }
        });
原文地址:https://www.cnblogs.com/lori/p/3596356.html