简体   繁体   English

如何在 javascript 上放置可变日期?

[英]How do you put a variable date on javascript?

I've been trying to use the input data from my HTML to create a date object but no mater what I tweak, it either shows invalid date, or gives today's date.我一直在尝试使用 HTML 中的输入数据来创建日期 object 但无论我如何调整,它要么显示无效日期,要么给出今天的日期。 Any help on how I can solve this?关于如何解决这个问题的任何帮助? Or corrections on what I may be doing wrong?或者更正我可能做错了什么? Thanks.谢谢。 Here's my code below.下面是我的代码。

    <input type="date" id="date">
    <script>
    var date = document.getElementById("date").value();
    var d = new Date(d);
    </script>

You put wrong.value() change to.value.你把 wrong.value() 改成 .value。

You can use momentjs to format date YYYY-MM-DD您可以使用 momentjs 格式化日期 YYYY-MM-DD

function check(){
var date = document.getElementById("date").value;
    var d = new moment(date, 'YYYY-MM-DD');
    console.log(d)
}

 function check(){ var date = document.getElementById("date").value; var d = new moment(date, 'YYYY-MM-DD'); console.log(d) }
 <input type="date" id="date" onblur="check()"> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>

May be helpful to you:可能对您有帮助:

 function x(){ var date = document.getElementById("date").value; var d = new Date(date); console.log(d); }
 <input type="date" id="date"> <button onclick="x()">show</button>

You can try the following:您可以尝试以下方法:

<!DOCTYPE html>
<html>
<head>
<title>Date experiment</title>

<script>
function processDate(mdate){
    console.log(mdate); 
    }
</script>
</head>
<body>
<p>This is a paragraph.</p>
<form>
  Date : <br>
  <input type="date" id="start" name="mydate" onchange="processDate(this.value)"
       value="2019-10-20" min="2019-10-01" max="2019-12-31">
</form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM