如何将html5输入类型的日期和时间转换为javascript datetime [英] How to convert html5 input type date and time to javascript datetime

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

问题描述

我正在使用html5输入类型:日期和时间。
如何将表单输入类型转换为javascript对象日期(包括其中的时间)?

I'm working with html5 input types: date and time. How to convert the form input type to javascript object Date (that includes time in it)?

以下是我的代码的一部分:

Here is a part of my code:

<html>
    <head>
        <script type="text/javascript">
             function GetDate(i_Date, i_Time)
             {
                  var theDate = new Date();
                  ....
             }
        </script>
    </head>
    <body>
        <form name="form_task">
             Date:<input type="date" name="task_date" />
             Time:<input type="time" name="task_time" />
             <input type="button" onclick="GetDate(task_date.value, task_time.value)" />
        </form>
    </body>
</html>


推荐答案

您需要做的就是从两者中提取值输入,连接它们,然后将它们传递给日期对象。

All you need to do is pull the value from both inputs, concatenate them, then pass them to a date object.

小提琴: http://jsfiddle.net/P4bva/

Date:<input id="date" type="date" name="task_date" />
Time:<input id="time" type="time" name="task_time" />
<button id="calc">Get Time</button>



JS



JS

var calc = document.getElementById("calc")

calc.addEventListener("click", function() {
    var date = document.getElementById("date").value,
        time = document.getElementById("time").value

    console.log(new Date(date + " " + time))
})

这篇关于如何将html5输入类型的日期和时间转换为javascript datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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