格式化Javascript日期时对象预期的错误

时间:2015-08-20 20:52:49

标签: javascript date-formatting

我用数据库中的信息动态填充表格,我想使用原型给Date对象一些格式。因此,在我的“addRows”方法中,使用Fecha.Value(Fecha.Value是一个日期)完成cell1,正确显示数据但未格式化。我尝试然后调用原型函数“formatDate”,但错误消息是“Object expectedObject expected”

我的代码:

function addRows(tableID, Fecha,CallerId,Documento,Agente,NombreEncuesta,Pregunta,Respuesta) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
//var row = document.createElement('tr');

var cell1 = row.insertCell(0);
cell1.innerHTML = Fecha.Value;  

var fechaValue = Fecha.Value;
var fechaFormato = fechaValue.formatDate("yyyy-MM-dd HH:mm");

var cell2 = row.insertCell(1);
cell2.innerHTML = CallerId;

var cell3 = row.insertCell(2);
cell3.innerHTML = Documento;

var cell4 = row.insertCell(3);
cell4.innerHTML = Agente;

var cell5 = row.insertCell(4);
cell5.innerHTML = NombreEncuesta;

var cell6 = row.insertCell(5);
cell6.innerHTML = Pregunta;

var cell7 = row.insertCell(6);
cell7.innerHTML = Respuesta;

table.tBodies[0].appendChild(row);

}

   Date.prototype.formatDate = function(format)
{
    var date = this;
    if (!format)
      format="MM/dd/yyyy";               

    var month = date.getMonth() + 1;
    var year = date.getFullYear();    

    format = format.replace("MM",month.toString().padL(2,"0"));        

    if (format.indexOf("yyyy") > -1)
        format = format.replace("yyyy",year.toString());
    else if (format.indexOf("yy") > -1)
        format = format.replace("yy",year.toString().substr(2,2));

    format = format.replace("dd",date.getDate().toString().padL(2,"0"));

    var hours = date.getHours();       
    if (format.indexOf("t") > -1)
    {
       if (hours > 11)
        format = format.replace("t","pm")
       else
        format = format.replace("t","am")
    }
    if (format.indexOf("HH") > -1)
        format = format.replace("HH",hours.toString().padL(2,"0"));
    if (format.indexOf("hh") > -1) {
        if (hours > 12) hours - 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh",hours.toString().padL(2,"0"));        
    }
    if (format.indexOf("mm") > -1)
       format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));
    if (format.indexOf("ss") > -1)
       format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));
    return format;
}

0 个答案:

没有答案