Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert html5 input type date and time to javascript datetime

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(date, 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>
like image 400
Gil Epshtain Avatar asked May 13 '14 19:05

Gil Epshtain


People also ask

What input type is included in datetime input html5?

Refers to supporting the following input types: `date`, `time`, `datetime-local`, `month` & `week`.

Which html5 attributes can be used with html5 date input type to limit date selection?

The max attribute specifies the maximum value (date) for a date field.

Which html5 input type allows users to select a date and time with time zone?

time: This input type allows the user to enter a time. datetime: This input type allows the user to select date and time along with timezone. datetime-local: This input type allows the user to select both local date and time. week: This input type allows the user to select week and year from the drop-down calendar.

How do you change date format to MM DD YYYY in HTML?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


1 Answers

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

Fiddle: http://jsfiddle.net/P4bva/

HTML

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

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))
})
like image 164
bottens Avatar answered Sep 20 '22 18:09

bottens