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>
                Refers to supporting the following input types: `date`, `time`, `datetime-local`, `month` & `week`.
The max attribute specifies the maximum value (date) for a date field.
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.
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.
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/
Date:<input id="date" type="date" name="task_date" />
Time:<input id="time" type="time" name="task_time" />
<button id="calc">Get Time</button>
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))
})
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With