11

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>

5 Answers 5

11

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))
})
Sign up to request clarification or add additional context in comments.

3 Comments

While it works in most browsers. It doesn't seem to work on Safari for some locale and input settings.
I can see it is working on iOS safari. Can you elaborate more on what locales or inputs don't work?
Works in Chrome and Firefox but not on IOS Safari. The javascript engine in Sarari can not parse new Date("2021-12-11 15:33") which is returns "Invalid Date" there.
1

That way you won't have the problem of having a date appear in the date field and the Date object has a different date.

let datainicio = $("#data-inicio").val();
let datafim = $("#data-fim").val();

if (!datainicio || !datafim) {
    bootbox.alert(DATA_INICIO_DATA_FIM_VAZIAS);
    return;
}

// the month is 0-indexed
let dateinicio = new Date(datainicio.slice(0, 4), Number(datainicio.slice(5, 7)) - 1, datainicio.slice(8, 10));
let datefim = new Date(datafim.slice(0, 4), Number(datafim.slice(5, 7)) - 1, datafim.slice(8, 10));

console.log("Date(datainicio): " + dateinicio);
console.log("Date(datafim) : " + datefim);

Comments

0

You could do something like this:

<!DOCTYPE html>
<style type="text/css">
label {
  width:150px;
  display:inline-block;
}
</style>

<label for="admissionDate">admission date</label>
<input id="admissionDate" name="admissionDate" type="date" />  
<br />
<label for="graduationDate">graduation date</label>
<input id="graduationDate" name="graduationDate" type="date" /> 
<br />
<label for="birthDate" >birth date</label>
<input id="birthDate" type="date" name="birthDate" />
<br />
<button id="age">Get Age</button>

<script type="text/javascript">
var doc = document;
var admission = doc.getElementById("admissionDate");
var graduation = doc.getElementById("graduationDate");
var birth = doc.getElementById("birthDate");
var age = doc.getElementById("age");

age.addEventListener("click", function(){
var gradDate = new Date(graduation.value);
var birthDate = new Date(birth.value);
var dateDiff = new Date(gradDate - birthDate);
var YEAR_OFFSET = 1970;
alert("This person is " + (dateDiff.getFullYear()-YEAR_OFFSET) + " years old");
});
</script>

Comments

0

**this how I did it **

   var calc = document.getElementById("calc")

calc.addEventListener("click", function() {
    var date = document.getElementById("date").value,
        time = document.getElementById("time").value
    
   var hi = new Date(date + " " + time).toISOString();

// Set the date we're counting down to
var countDownDate = new Date(hi).getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();
    
  // Find the distance between now and the count down date
  var distance = countDownDate - now;
    
  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
    
  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "وقت المهمة حان  ";
  }
}, 1000);})
<p id="demo"></p>
Date:<input id="date" type="date" name="task_date" />
Time:<input id="time" type="time" name="task_time" />
<button id="calc">Get Time</button>
</div>

Comments

-1

Try this

<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</script>
</head>

<body onload="startTime()">

<div id="txt"></div>

</body>
</html>

1 Comment

how is this related to the question? I've asked how to parse input-type-data and input-type-time into JS Date() object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.