在javascript中输入类型的日期和时间中设置值 [英] Set values in input type date and time in javascript

查看:214
本文介绍了在javascript中输入类型的日期和时间中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个输入字段:

日期:

<input type="date" id="currentDate">

时间:

<input type="time" id="currentTime" >

我正在测试html5的那些新功能. 如何在这些字段中设置当前值. 我很难通过javascript在这些字段中设置值.

I'm just testing those new features of html5. How to set current values in these fields. I am having a hard time setting values in those fields through javascript.

推荐答案

您只需使用相应的格式设置输入字段的value:

You can just set the value of the input field with the respective formats:

dateyyyy-MM-dd

timeHH:mm

使用您的示例,您可以执行以下简单操作:

Using your example, you can do something simple like:

var date = new Date();
var currentDate = date.toISOString().slice(0,10);
var currentTime = date.getHours() + ':' + date.getMinutes();

document.getElementById('currentDate').value = currentDate;
document.getElementById('currentTime').value = currentTime;

这篇关于在javascript中输入类型的日期和时间中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆