js 日期格式化

Date.prototype.Format = function(fmt)
    { //author: meizz
        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;
    };

    var time2 = new Date("2015-03-21").Format("yyyy-MM-dd");

//获取客户端当前时间
function getDate(){
    var now =new Date();
    var year=now.getFullYear();
    var month=now.getMonth();
    var day=now.getDate();
    var hours=now.getHours();
    var minutes=now.getMinutes();
    var seconds=now.getSeconds();
    return ""+year+"-"+month+"-"+day+"  "+hours+":"+minutes+":"+seconds+"";
};

 

 

//mysql返回的时间处理
//格式化服务器时间
function formatTime(time){
    debugger;
    var year=time.year-100;
    var month=time.month+1;
    return '20'+year+'-'+month+'-'+time.date+' '+time.hours+':'+time.minutes+':'+time.seconds;
};

 

posted @ 2015-03-23 10:37  RY一步一个脚印  阅读(336)  评论(0编辑  收藏  举报