简体   繁体   English

在表单输入字段中自动插入日期和时间?

[英]Auto insert date and time in form input field?

Using javascript, how can we auto insert the current DATE and TIME into a form input field.使用 javascript,我们如何将当前日期和时间自动插入到表单输入字段中。

I'm building a "Problems" form to keep track of user submitted complaints about a certain service.我正在构建一个“问题”表单来跟踪用户提交的关于某项服务的投诉。 I'm collecting as much info as possible.我正在收集尽可能多的信息。 I want to save the staff here from having to manually enter the date and time each time they open the form.我想让这里的员工每次打开表格时都不必手动输入日期和时间。

I have this snippet of code:我有这段代码:

<input id="date" name="date" value="javascript:document.write(Date()+'.')"/>

but it's not working.但它不工作。

many thanks for any help.非常感谢您的帮助。

Javascript won't execute within a value attribute. Javascript 不会在值属性内执行。 You could do something like this, though:不过,您可以这样做:

<input id="date" name="date">

<script type="text/javascript">
  document.getElementById('date').value = Date();
</script>

You'd probably want to format the date as you prefer, because the default output of Date() looks something like: Tue Jun 16 2009 10:47:10 GMT-0400 (Eastern Daylight Time) .您可能希望根据自己的喜好设置日期格式,因为 Date() 的默认值 output 类似于: Tue Jun 16 2009 10:47:10 GMT-0400 (Eastern Daylight Time) See this SO question for info about formatting a date.有关格式化日期的信息,请参阅此 SO 问题

<input type="date" id="myDate"  />

<script type="text/javascript">
function SetDate()
{
var date = new Date();

var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();

if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

var today = year + "-" + month + "-" + day;


document.getElementById('myDate').value = today;
}
</script>

<body onload="SetDate();">

found here: http://jsbin.com/oqekar/1/edit?html,js,output在这里找到: http://jsbin.com/oqekar/1/edit?html,js,output

See the example, http://jsbin.com/ahehe看例子, http://jsbin.com/ahehe

Use the JavaScript date formatting utility described here .使用此处描述的 JavaScript 日期格式化实用程序。

<input id="date" name="date" />

<script>
   document.getElementById('date').value = (new Date()).format("m/dd/yy");
</script>

The fastest way to get date formatting thats any good is to use datejs .获取日期格式的最快方法是使用datejs

The lib is really easy to use and does give you very pretty formatting.该库非常易于使用,并且确实为您提供了非常漂亮的格式。


as far as your code snippet, dont use document.write if you can help it.就您的代码片段而言,如果可以的话,请不要使用 document.write。 try尝试

 document.getElementById("date").value = new Date().toUTCString(); 
$("#startDate").val($.datepicker.formatDate("dd/mm/yy", new Date()));
$("#endDate").val($.datepicker.formatDate("dd/mm/yy", new Date()));

Add the above code at the end of the script.在脚本末尾添加上面的代码。 This is required because the datepicker plugin has no provision to set the date in the control while initializing.这是必需的,因为日期选择器插件没有规定在初始化时在控件中设置日期。

It sounds like you are going to be storing the value that input field contains after the form is submitted, which means you are using a scripting language.听起来您将在提交表单后存储输入字段包含的值,这意味着您正在使用脚本语言。 I would use it instead of JavaScript as most scripting languages have better time/date formatting options.我会使用它而不是 JavaScript,因为大多数脚本语言都有更好的时间/日期格式化选项。 In PHP you could do something like this:在 PHP 你可以这样做:

<input id="date" name="date" value="<?php echo date("M j, Y - g:i"); ?>"/>

Which would fill the field with "Jun 16, 2009 - 8:58"这将用“2009 年 6 月 16 日 - 8:58”填充该字段

If you are using HTML5 date如果您使用的是 HTML5 日期

use this code使用此代码

HTML HTML

<input type="date" name="bday" id="start_date"/>

Java Script Java 脚本

document.getElementById('start_date').value = Date();
<input id="date" name="date" />
<script type="text/javascript">
document.getElementById("date").value = new Date();
</script>

Another option is:另一种选择是:

<script language="JavaScript" type="text/javascript"> 
document.getElementById("my_date:box").value = ( Stamp.getDate()+"/"+(Stamp.getMonth() + 1)+"/"+(Stamp.getYear()) );
</script>
var date = new Date();

document.getElementById("date").value = date.getFullYear() + "-" + (date.getMonth()<10?'0':'') + (date.getMonth() + 1) + "-" + (date.getDate()<10?'0':'') + date.getDate();

document.getElementById("hour").value = (date.getHours()<10?'0':'') + date.getHours()  + ":" + (date.getMinutes()<10?'0':'') + date.getMinutes();

Im not do not know all of the technical language but I could not find the answer anywhere so I came up with this and it worked... Good Luck!我不是不知道所有的技术语言,但我在任何地方都找不到答案,所以我想出了这个并且它奏效了……祝你好运!

$time = date("Y/m/d h:i:s");

$sql = "INSERT INTO *yourtablenamehere* ('dt')  VALUES ('$time')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM