将日期自动填充到表单字段中
Posted
技术标签:
【中文标题】将日期自动填充到表单字段中【英文标题】:Autofill Date Into Form Field 【发布时间】:2017-03-22 08:24:59 【问题描述】:我正在尝试在我的网络表单中自动填充“日期”字段,这样用户就不必输入它。
目前,我正在使用下面的代码,它工作正常,除了它没有以正确的格式提供日期,而且它还包括我不想包括的时间。
<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>
上面的代码产生这样的结果:Wed Mar 22 2017 00:59:21 GMT-0700 (Pacific Daylight Time)
我宁愿结果是这样的:2017 年 3 月 22 日,星期三
【问题讨论】:
查看jsfiddle 【参考方案1】:似乎您正在分配日期值而不是计算的字符串。
查看下面的示例以了解差异。
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);
<div id="clockbox"></div>
<div id="clockbox1"></div>
【讨论】:
谢谢!完美! 不幸的是,在发布这个问题时......我没有意识到我实际上正在处理我网站上的两个不同页面,它们都需要更改日期格式。我将您的代码保存在一个 php 文件中,并且可以使用“包含”代码来更正我的一个页面上的日期格式。但是....我没有在我的问题中提供正确的代码来解决自动填充的表单字段。表单域中的当前代码是: 这也产生了错误的格式。【参考方案2】:你可以改变你的功能:
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);
【讨论】:
有什么变化?? 使用 toDateString() 将日期更改为 dateString; 一个好的答案将解释 OP 的问题以及您的代码如何修复它。【参考方案3】:检查这个简单的方法;
<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>
【讨论】:
你不能像document.getElementById('clockbox').innerHTML('value')
;这样赋值。应该是document.getElementById('clockbox').innerHTML = 'value';
以上是关于将日期自动填充到表单字段中的主要内容,如果未能解决你的问题,请参考以下文章