自动填充日期到表单字段

时间:2017-03-22 08:25:00

标签: javascript php forms autofill

我正在尝试在我的网络表单中自动填充“日期”字段,以便用户无需输入。

目前,我正在使用下面的代码,它工作正常,除了它没有以正确的格式提供日期,它还包括我不想包括的时间。

    <script>
       tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
       tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
       function GetClock(){ 
            var d=new Date(); 
            var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear();
            if(nyear<1000) nyear+=1900;
            document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+""; } window.onload=function(){ GetClock(); setInterval(GetClock,1000); 
        }

     </script>

上面的代码产生如下结果:2017年3月22日星期三00:59:21 GMT-0700(太平洋夏令时)

我希望结果是这样的:2017年3月22日星期三

3 个答案:

答案 0 :(得分:1)

似乎您正在分配日期值而不是计算字符串。

检查以下示例中的差异。

&#13;
&#13;
tday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

tmonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function GetClock() {
  var d = new Date();
  var nday = d.getDay(),
    nmonth = d.getMonth(),
    ndate = d.getDate(),
    nyear = d.getYear();
  if (nyear < 1000)
    nyear += 1900;

  // Assign the direct date value
  document.getElementById('clockbox').innerHTML = d;

  //Assigning the customized date value
  document.getElementById('clockbox1').innerHTML = tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + "";;
}

window.onload = function() {
  GetClock();
  setInterval(GetClock, 1000);
}
&#13;
<div id="clockbox"></div>
<div id="clockbox1"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以更改以下功能:

tday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    tmonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    function GetClock() {
        var d = new Date();
        var nday = d.getDay(), nmonth = d.getMonth(), ndate = d.getDate(), nyear = d.getYear();
        if (nyear < 1000) nyear += 1900;
        var date = tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear;
        var dateString = date.toDateString();
        document.getElementById('clockbox').innerHTML = dateString;
    }
    window.onload = function () {
        GetClock(); setInterval(GetClock, 1000);
    }

答案 2 :(得分:0)

检查这个简单的方法;

<script>

     tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
     tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
     var d=new Date();
     document.getElementById('clockbox').innerHTML=tday[d.getDay()]+"  "+tmonth[d.getMonth()]+" "+d.getDate()+","+d.getFullYear();

</script>