js转换数据库DateTime字段类型

在程序中,从数据库中读取到的日期时间类型数据一般是这种格式:"/Date(139832079+0800)/"

我们要让它转换为这种形式:'2012-12-10 11:05:21'

可用如下js函数:

function timeFormatter(value) {

    var t = new Date(parseInt(value.replace("/Date(", "").replace(")/" , "").split( "+")[0]));

    return t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();

}


如果需要长日期长时间格式:
timeFormatter: function (value) {
        var datetime = new Date(parseInt(value.replace("/Date(", "").replace(")/", "").split("+")[0]))        
        var year = datetime.getFullYear(); 
        var month = datetime.getMonth()+1;//js从0开始取 
        var date = datetime.getDate(); 
        var hour = datetime.getHours(); 
        var minutes = datetime.getMinutes(); 
        var second = datetime.getSeconds();
 
        if(month<10){
            month = "0" + month;
        }
        if(date<10){
            date = "0" + date;
        }
        if(hour <10){
            hour = "0" + hour;
        }
        if(minutes <10){
            minutes = "0" + minutes;
        }
        if(second <10){
            second = "0" + second ;
        }
 
        var time = year+"-"+month+"-"+date+" "+hour+":"+minutes+":"+second; //2009-06-12 17:18:05
        // alert(time);
        return time;

    }

你可能感兴趣的:(JavaScript)