简体   繁体   English

如何使用 javascript 从日期输入中获取日期

[英]how to get a date from a date input using javascript

hello so i have a javascript function that takes two dates from two date inputs, compares them and returns a boolean value except it doesn't work你好,所以我有一个 javascript function 从两个日期输入中获取两个日期,比较它们并返回一个 boolean 值,除非它不起作用

 function checkdate() { var datefrom = new Date(); var dateto = new Date(); datefrom = getElementById("date_from").innerHTML; dateto = getElementById("date_to").innerHTML; if (datefrom > dateto) { alert("Dateto must be bigger than datefrom "); return false; } else return true; }
 <form onsubmit="return checkdate()" class="forms-sample" method="post" action="addstage.php"> <div class="form-group row"> <label for="exampleInputUsername2" class="col-sm-3 col-form-label">Date Début</label> <div class="col-sm-9"> <input type="date" class="form-control" id="date_from" placeholder="Debut" name="datedebut" required="required"> </div> </div> <div class="form-group row"> <label for="exampleInputEmail2" class="col-sm-3 col-form-label">Date Fin</label> <div class="col-sm-9"> <input type="date" class="form-control" id="date_to" placeholder="Fin" name="datefin" required="required"> </div> </div> <div class="form-check form-check-flat form-check-primary"></div> <button type="submit" class="btn btn-primary mr-2" value="addStage" name="action">Add</button> </form>

Two problems:两个问题:

  1. You're calling getElementById globally, but should be calling it on the document object.您正在全局调用getElementById ,但应该在document object 上调用它。
  2. You're using .innerHtml instead of .value您正在使用.innerHtml而不是.value

As an aside you also don't need to create new Date() objects just to throw them away.顺便说一句,您也不需要创建new Date()对象只是为了扔掉它们。 Simply set the values you want right away:只需立即设置您想要的值:

var datefrom = document.getElementById("date_from").value;
var dateto = document.getElementById("date_to").value;

Note: These values are strings , not dates .注意:这些值是字符串,而不是日期 According to MDN :根据MDN

The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser , but the parsed value is always formatted yyyy-mm-dd.显示的日期格式将与实际值不同- 显示的日期是根据用户浏览器的区域设置格式化的,但解析后的值始终是 yyyy-mm-dd 格式。

So for comparison purposes this should still work, since yyyy-mm-dd is an incrementing value.因此,出于比较目的,这应该仍然有效,因为yyyy-mm-dd是一个递增值。 But if you need a Date object then MDN also says there's a .valueAsDate property (though has no example on that page), as well as a .valueAsNumber which returns a UNIX timestamp that should be reliable for creating a Date .但是,如果您需要Date object 那么 MDN 还说有一个.valueAsDate属性(尽管该页面上没有示例),以及一个返回 UNIX 时间戳的.valueAsNumber ,该时间戳对于创建Date应该是可靠的。

Otherwise dates can be tricky in JavaScript, and libraries like Moment.js exist to make that easier.否则 JavaScript 中的日期可能会很棘手,而Moment.js之类的库的存在使这更容易。

You can't use getElementById as a static method.您不能将getElementById用作static方法。 You have to call it on document :您必须在document上调用它:

datefrom= document.getElementById("date_from").innerHTML;
dateto= document.getElementById("date_to").innerHTML;

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

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