如何将html5输入类型的日期和时间转换为javascript datetime

时间:2014-05-13 19:52:08

标签: javascript html5 datetime

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

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

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

5 个答案:

答案 0 :(得分:5)

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

小提琴: 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))
})

答案 1 :(得分:0)

你可以这样做:

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

答案 2 :(得分:0)

**这是我的做法**

   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>

答案 3 :(得分:0)

dessa maneira voce não tera o problema de aparecer uma data no campo de data e no objeto Date estar com uma data different.

这样你就不会有日期出现在日期字段中而日期对象具有不同日期的问题。

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

答案 4 :(得分:-1)

试试这个

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