简体   繁体   English

表格自动填写时间和日期

[英]Form autofill time and date

I am trying to autofill the time and date of an HTML form and need to use UTC time.我正在尝试自动填写 HTML 表格的时间和日期,并且需要使用 UTC 时间。 I would like to use PHP or javascript.我想使用 PHP 或 javascript。

 <form> <label>Date</label> <input type="date" name="date" value="<?php echo date('Ym-d'); ?>"> <br /> <label>Time</label> <input type="time" name="time"/ value=""> </form>
Thanks in advance. 提前致谢。

You just need to add <?= date('H:i'); ?>您只需要添加<?= date('H:i'); ?> <?= date('H:i'); ?> in your time input. <?= date('H:i'); ?>在你的时间输入。

<input type="time" name="time"/ value="<?= date('H:i'); ?>">

For explanation:解释:

<?=date('H:i');?> is the same like: <?php echo date('H:i');?> <?=date('H:i');?>类似: <?php echo date('H:i');?>

And the 'H' are the hours in 24-hour-format and 'i' are the minutes with leading zeros. 'H' 是 24 小时格式的小时,'i' 是带前导零的分钟。

You can take a look at https://www.php.net/manual/en/function.date.php for different possibilities to format a date.您可以查看https://www.php.net/manual/en/function.date.php以了解格式化日期的不同可能性。

With JavaScript, the Date object is used to retrieve the time & date.对于 JavaScript, 日期object 用于检索时间和日期。 To pre-populate your two input fields with the time and date respectively you first need to get a reference to them to be able to manipulate it using JavaScript.要分别使用时间和日期预先填充两个输入字段,您首先需要获取对它们的引用,以便能够使用 JavaScript 对其进行操作。

Both of the input fields have it's name tag set, so we can use this.两个输入字段都设置了名称标签,因此我们可以使用它。

var inputElementTime=document.getElementsByName("time")[0];
var inputElementDate=document.getElementsByName("date")[0];

The function getElmentsByName returns an array, since there can be multiple elements with the same name. function getElmentsByName 返回一个数组,因为可以有多个具有相同名称的元素。 In your case there's just one so we can directly use the first element of the array by appending [0] .在您的情况下,只有一个,因此我们可以通过附加[0]直接使用数组的第一个元素。

Now we need to get the actual date:现在我们需要获取实际日期:

var now=new Date();

The input elements .value property needs to be formatted in a special way.输入元素的.value属性需要以特殊方式格式化。 For the time it should be hh:mm and for the date: yyyy-mm-dd时间应该是hh:mm和日期: yyyy-mm-dd

Note: hh, mm, and dd always need to have 2 digits.注意: hh、mm 和 dd 总是需要有 2 位数字。 If it's the first of June you have to add a leading zero eg 01 .如果是 6 月 1 日,则必须添加前导零,例如01

Here's the complete example (just click on 'Run code snippet'):这是完整的示例(只需单击“运行代码片段”):

 var now = new Date(); var inputElementTime = document.getElementsByName("time")[0]; var inputElementDate = document.getElementsByName("date")[0]; inputElementTime.value = ("0" + now.getHours()).slice(-2) + ":" + ("0" + now.getMinutes()).slice(-2); inputElementDate.value = now.getUTCFullYear() + "-" + ("0" + now.getMonth()).slice(-2) + "-" + ("0" + now.getDate()).slice(-2);
 <form> <label>Date</label> <input type="date" name="date"> <br /> <label>Time</label> <input type="time" name="time"> </form>

Here is a way using Date();这是一种使用 Date() 的方法; funtion功能

 let objectDate = new Date(); var time = objectDate.toLocaleTimeString([], { hourCycle: 'h24', hour: '2-digit', minute: '2-digit' }); var date = objectDate.toLocaleDateString('sv'); document.getElementById('time').value = time; document.getElementById('datePicker').value = date;
 <div class="form-check form-check-inline"> <label for="appt">Time:</label> <input type="time" id="time" name="appt" min="09:00" max="18:00" required> </div> <label for="start">Date:</label> <input type="date" id="datePicker" value="2018-07-22">

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

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