目录

前端接受后端JSON格式日期数据如何转化为时间YYYY-mm-dd-HHMMSS形式

目录

前端接受后端JSON格式日期数据如何转化为时间YYYY-mm-dd HH:MM:SS形式

后端传输JSON包装的数据 ,日期变成这样的格式

参数名:{

day:8

month:1

……

}

,想要转换成2019-08-10的形式显示在前端怎么办呢?

/日期转换
Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds()
        //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


function toDate(obj) {
    var date = new Date();
    date.setTime(obj.time);
    date.setHours(obj.hours);
    date.setMinutes(obj.minutes);
    date.setSeconds(obj.seconds);
    return date.format("yyyy-MM-dd hh:mm:ss"); //调用Date.prototype.format方法
}
function  toHoursAndMinutes(obj) {
    var date = new Date();
    date.setHours(obj.hours);
    date.setMinutes(obj.minutes);
    return date.format("hh:mm"); //调用Date.prototype.format方法
}

话不多说 直接贴代码,实际调用只需要将得到的JSON格式日期传入函数,函数中设置后需要的年月日或时分秒,再转换后返回值便是想要的数据样子了