请教两个问题
1.JS读取数据库中的时间型数据 直接现实的话是 1Mon Apr 28 00:00:00 UTC+0900 2008 我怎么把它转换成 2008-04-28
2.JS读取数据库实现后 在本机上可以正常读取,但是我在别的机器上面访问这个页面 提示 “未发现数据源名称并且为指定驱动程序”

解决方案 »

  1.   


    s = "Mon Apr 28 00:00:00 UTC+0800 2008";
    s = fDate(s);
    alert(s)
    function fDate(s){
      var dt = new Date(s);
      var y = dt.getFullYear();
      var m = dt.getMonth()+1;
      var d = dt.getDate();
      m = m<10?'0'+m:m;
      d = d<10?'0'+d:d;
      return y +"-"+m+"-"+d;
    }
    2 数据库位于何处?其他机器是否装了数据库的ODBC驱动?
      

  2.   

    <script>
    var d = new Date("Mon Apr 28 00:00:00 UTC+0900 2008");//东9区时间
    alert(d);//东8区应该是2008-04-27 23:00:00
    //LZ如果想要东9区时间,则加1小时即可
    d.setTime(d.getTime()+60*60*1000);alert(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate());
    </script>第二个问题描述不是很清楚
      

  3.   

    数据库我自己的机器上面其他的机器没有安装ODBC驱动,我自己的机器安装了 
      

  4.   

    js是在客户端运行的,运行环境和客户端相关,
    如果不希望客户端安装,应该采用数据在服务器端,用asp,php,jsp等技术读取的方式.
      

  5.   

    可以给Date对象加一个格式化方法<script>
    Date.prototype.format = function(format)
    {
        var o =
        {
            "M+" : this.getMonth()+1, //month
            "d+" : this.getDate(),    //day
            "h+" : this.getHours(),   //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format)){
         format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o){
         if(new RegExp("("+ k +")").test(format)){
         format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
         }
        }
        return format;
    }
    var d = new Date() ;
    alert(d.format("yyyy")) ;          //年
            alert(d.format("MM")) ;            //月
            alert(d.format("dd")) ;            //日
            alert(d.format("qq")) ;            //季度
            alert(d.format("yyyy-MM-dd")) ;    //年月日
    </script>