zoukankan      html  css  js  c++  java
  • 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'));
     

    结果:

  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/qiufengke/p/5729278.html
Copyright © 2011-2022 走看看