0

I retrieve a list of date from a DB using JS and ADO and I show it in a table with the following code:

var detDate = new Date() ;
      if(!rsDetail.bof) {
        rsDetail.MoveFirst()
        while(!rsDetail.eof) {
            detDate  = rsDetail.fields(2).value; 
            rsDetail.MoveNext()
        }
...  TableHtml += '<td>' + detDate + '</td>' ...

The output looks like that: Sat Oct 15 00:00:00 EDT 2011
I want it to look like that: 2011-10-15
I have been looking around at the JS date object and I tried to do formatDate(detDate,'y-mm-dd') but it doesn't work...

4

1 回答 1

1

您可以像这样格式化您的日期对象:

function formatDate(dt) {
    var pad = function(str, c, width) {
        while (str.length < width)
            str = c + str;

        return str;
    }

    var dateString = dt.getFullYear() + "-" + pad((dt.getMonth() + 1).toString(), '0', 2) + "-" +pad( dt.getDate().toString(), '0', 2);

    return dateString;       
}

tableHTML += "<td>" + formatDate(debDate) + "</td>";
于 2011-11-15T14:54:40.807 回答