本文介绍了使用datetime作为输入来检查日期是当前日期还是将来的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查在 datetime
字段中输入的日期是当前日期还是将来的日期。我尝试过:
I'm trying to check if the date entered in datetime
field is a current or a future date. I've tried:
function validations(){
var value=document.getElementById("showdate").value;
if (new Date() > new Date(value)) {
alert("Past date");
}
}
<Form method="post" onsubmit="validations()" autocomplete>
<input type="datetime-local" name="showdate" class="right" required="required" id="showdate">
<input type="submit">
</form>
但是此代码仅适用于 date
字段。
But this code will work only with the date
field.
推荐答案
通常,在JS中,要比较日期,您应该尝试:
Generally, in JS, to compare dates you should try:
function isFutureDate(value) {
d_now = new Date();
d_inp = new Date(value)
return d_now.getTime() <= d_inp.getTime();
}
在 getTime Date
对象的c $ c>函数。
Run regular comparison operators on the getTime
function of a Date
object.
这篇关于使用datetime作为输入来检查日期是当前日期还是将来的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!