js格式化日期


日期类型:/Date(1406822400000)/

function extend_Todecimal(str, count) {
    if (str==undefined || str==""||str==null||str==NaN) {
        return 0;
    }
    var vv = Math.pow(10, count);
    return Math.round(str * vv) / vv;
}
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
function extend_formatDateTime(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    }
    else {
        dt = new Date(value);
        if (isNaN(dt)) {
            value = value.replace(/\/Date\((-?\d+)\)\//, '$1');
            dt = new Date();
            dt.setTime(value);
        }
    }
    return dt.format("yyyy-MM-dd hh:mm");
}
function extend_formatDate(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    }
    else {
        dt = new Date(value);
        if (isNaN(dt)) {
            value = value.replace(/\/Date\((-?\d+)\)\//, '$1');
            dt = new Date();
            dt.setTime(value);
        }
    }
    return dt.format("yyyy-MM-dd");
}


function extend_formatDateMmDd(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    }
    else {
        dt = new Date(value);
        if (isNaN(dt)) {
            value = value.replace(/\/Date\((-?\d+)\)\//, '$1');
            dt = new Date();
            dt.setTime(value);
        }
    }
    return dt.format("MM-dd");
}


$.extend(
    $.fn.datagrid.defaults.editors, {
        my97: {
        init: function (container, options) {
            var input = $('<input class="Wdate" onclick="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true});"  />').appendTo(container);
            return input;
        },
        getValue: function (target) {
            return $(target).val();
        },
        setValue: function (target, value) {
            $(target).val(extend_formatDate(value));
        },
        resize: function (target, width) {
            var input = $(target);
            if ($.boxModel == true) {
                input.width(width - (input.outerWidth() - input.width()));
            } else {
                input.width(width);
            }
        }
    },
        datebox: {
            init: function (container, options) {
                var input = $('<input type="text">').appendTo(container);
                input.datebox(options);
                return input;
            },
            destroy: function (target) {
                $(target).datebox('destroy');
            },
            getValue: function (target) {
                return $(target).datebox('getValue');
            },
            setValue: function (target, value) {
                $(target).datebox('setValue', formatDatebox(value));
            },
            resize: function (target, width) {
                $(target).datebox('resize', width);
            }
        }
    },
    {
        extend_getContentByType: function (type) {
            if (type == "加载")
                return "数据正在加载中...";
            else if (type == "无数据")
                return "没有找到相关数据!";
        },
        extend_extend_getUrlVars: function () {
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        },
        extend_getUrlVar: function (name) {
            return $.extend_extend_getUrlVars()[name];
        },
        extend_getCheckBoxValues: function (checkName) {
            var weeks = '';
            var $sel = $("input[name='" + checkName + "']:checked");
            if ($sel.length > 0) {
                var selectedItems = new Array();
                $sel.each(function () { selectedItems.push($(this).val()); });
                weeks = selectedItems.join(',')
            }
            return weeks;
        },
        extend_formatterdatebytype: function (val, type) {
            var NewDtime = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
            var Dyear = NewDtime.getFullYear();
            var Dmonth = NewDtime.getMonth() + 1;
            var Ddate = NewDtime.getDate();
            // NewDtime.getMonth() + 1, //month 
            // NewDtime.getDate(),    //day 
            //NewDtime.getHours(),   //hour 
            //NewDtime.getMinutes(), //minute 
            //NewDtime.getSeconds(), //second 
            //Math.floor((NewDtime.getMonth() + 3) / 3),  //quarter 
            //NewDtime.getMilliseconds() //millisecond  
            try {
                return type.replace("yyyy", Dyear).replace("MM", Dmonth).replace("dd", Ddate);
            } catch (e) {
                return Dyear + "-" + Dmonth + "-" + Ddate;
            }
        }
    }
);

使用:

extend_formatDateMmDd(a.BeginDate) 

你可能感兴趣的:(js格式化日期)