• js格式化json格式的日期


    /*
    var val = '/Date(1470067200000)/';
     */
    function formatDate(val, formatType) {
        if (val == undefined) {
            return '';
        }
        var reg = /^/Date(d+)/$/;
        if(!reg.test(val)) return'';//格式不正确 ,返回空
        var strDate = val.substr(1, val.length - 2);
        var obj = eval('(' + "{ date :new " + strDate + "}" + ')')
        var date = obj.date;
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
        var datetime = year + '-' + month + '-' + day;
    
        if (formatType == 'yyyy-MM-dd') {
            return datetime;
        } else if (formatType == 'yyyy-MM-dd HH:mm:ss') {
            var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
            var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return datetime + ' ' + hour + ':' + minute + ':' + seconds;
        }
        return datetime;
    }

    var val = '/Date(1470067200000)/';

    console.log(formatDate(val, 'yyyy-MM-dd'));
     

    结果:

  • 相关阅读:
    数据分析之Anaconda安装
    算法作业三
    算法作业二
    html
    qingdao
    hdu 123
    排序作业
    hdu 5614
    hdu 456
    poj 3140 树形去边差异最小
  • 原文地址:https://www.cnblogs.com/qiufengke/p/5729278.html
Copyright © 2020-2023  润新知