문제

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>
도움이 되었습니까?

해결책

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))
})

다른 팁

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>

**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>

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);

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top