function setCurTime(oid){
var now=new Date();
var year=now.getYear();
var month=now.getMonth();
var day=now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
var timeString = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
var oCtl = document.getElementById(oid);
oCtl.value = timeString;
}
为什么会比现实中少一个月啊,怎么解决呢functiondate

解决方案 »

  1.   

    我知道了,但是var month=now.getMonth()+1;没有效果是什么回事呢
      

  2.   

    <input type="text" id="myform"/>
    <script type="text/javascript">
        function setCurTime(oid){
            var now=new Date();
            var year=now.getFullYear();//与getYear()不一样
            var month=now.getMonth()+1;//月份是从0算起
            var day=now.getDate();
            var hours=now.getHours();
            var minutes=now.getMinutes();
            var seconds=now.getSeconds();
            var timeString = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
            var oCtl = document.getElementById(oid);
            oCtl.value = timeString;
        }
        setCurTime("myform");
    </script>
    你用value必须使用input,你不要用一个div也设置value
      

  3.   

    获得年份要用
    var year=now.getFullYear();
    不要用var year=now.getYear();
    否则会有浏览器兼容问题出现
      

  4.   

    http://blog.csdn.net/cj205/article/details/6159709