如何从日历javascript - HTML5中获取日期,月份,年份值

时间:2013-01-22 03:35:23

标签: javascript html5

我有一个日历,当我从javascript中选择一个日期来检查一些我曾选择告诉我它是否属实的值。

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<head>
<title>SOMETHING</title>
    <script>
function check(){ 
                 if (document.getElementById("cinema").value == "1"){
                        var d = new date();
                        var x = document.getElementById("date");
                        x.innerHTML=d.getMonth();
                        if (x == "1"){
                        document.write("ok");
                        }
                        }
                        else {
                        alert('null');
                        }
                        }

</script>
</head>
<body>
<header>
        <h1>SOMETHING</h1>
    </header>
<br />
<div>
    <form>
    <fieldset>
                <label for="date">CHECK DATE</label>
                <input type="date" id="date" name="date" required="required"/>
        <br />
                <label for="time">CHECK TIME</label>
                <input type="time" id="time" name="time" required="required" />
    </fieldset>
</form>
</div>
<br />
<button onclick='check()'>Check</button>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您需要先为日期对象指定一个值,然后从中导出日期,月份。

var d = new date(document.getElementById("date"));
alert(d.getMonth())

答案 1 :(得分:0)

这里的东西可以让你走上正轨:

    function check()
    {
        var Temp= document.getElementById("date");

        var myDate = new Date(Temp.value);

        document.write(myDate);
        document.write("<br />");
        document.write(myDate.getMonth()); 
    }

答案 2 :(得分:0)

就是这样!

function check() {
  var a = document.getElementById("thedate").value;
  var check = "2013-01-20";
  if (a == check) {
    alert("YES");
  } else {
    alert("STOP");
  }
}
<meta charset="UTF-8">

<body>
  <input type="date" id="thedate">
  <button onclick="check()">Try it</button>
</body>