ADO 记录集中的 Javascript 日期格式

发布于 2024-12-15 19:12:53 字数 493 浏览 10 评论 0原文

我使用 JS 和 ADO 从数据库中检索日期列表,并使用以下代码将其显示在表中:

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

输出如下所示: Sat Oct 15 00:00:00 EDT 2011
我希望它看起来像这样:2011-10-15
我一直在查看 JS 日期对象,并尝试执行 formatDate(detDate,'y-mm-dd') 但它不起作用......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-12-22 19:12:53

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

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>";

You can format your date object like that :

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>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文