有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
你可以用最简单易懂的方式进行比较。
<input type="date" id="getdate1" />
<input type="date" id="getdate2" />
假设您有两个要比较的日期输入。
因此,首先编写一个通用的日期解析方法。
<script type="text/javascript">
function parseDate(input) {
var datecomp= input.split('.'); //if date format 21.09.2017
var tparts=timecomp.split(':');//if time also giving
return new Date(dparts[2], dparts[1]-1, dparts[0], tparts[0], tparts[1]);
// here new date( year, month, date,)
}
</script>
parseDate()是解析日期的常用方法。现在您可以检查date=、>、<任何类型的比较
<script type="text/javascript">
$(document).ready(function(){
//parseDate(pass in this method date);
Var Date1=parseDate($("#getdate1").val());
Var Date2=parseDate($("#getdate2").val());
//use any oe < or > or = as per ur requirment
if(Date1 = Date2){
return false; //or your code {}
}
});
</script>
当然,这段代码会帮助你。
其他回答
假设您处理的是2014年[:-/.]06[:-//.]06或2014年06月[:-/./.]06日格式,那么您可以这样比较日期
var a = '2014.06/07', b = '2014-06.07', c = '07-06/2014', d = '07/06.2014';
parseInt(a.replace(/[:\s\/\.-]/g, '')) == parseInt(b.replace(/[:\s\/\.-]/g, '')); // true
parseInt(c.replace(/[:\s\/\.-]/g, '')) == parseInt(d.replace(/[:\s\/\.-]/g, '')); // true
parseInt(a.replace(/[:\s\/\.-]/g, '')) < parseInt(b.replace(/[:\s\/\.-]/g, '')); // false
parseInt(c.replace(/[:\s\/\.-]/g, '')) > parseInt(d.replace(/[:\s\/\.-]/g, '')); // false
如您所见,我们去掉分隔符,然后比较整数。
你可以用最简单易懂的方式进行比较。
<input type="date" id="getdate1" />
<input type="date" id="getdate2" />
假设您有两个要比较的日期输入。
因此,首先编写一个通用的日期解析方法。
<script type="text/javascript">
function parseDate(input) {
var datecomp= input.split('.'); //if date format 21.09.2017
var tparts=timecomp.split(':');//if time also giving
return new Date(dparts[2], dparts[1]-1, dparts[0], tparts[0], tparts[1]);
// here new date( year, month, date,)
}
</script>
parseDate()是解析日期的常用方法。现在您可以检查date=、>、<任何类型的比较
<script type="text/javascript">
$(document).ready(function(){
//parseDate(pass in this method date);
Var Date1=parseDate($("#getdate1").val());
Var Date2=parseDate($("#getdate2").val());
//use any oe < or > or = as per ur requirment
if(Date1 = Date2){
return false; //or your code {}
}
});
</script>
当然,这段代码会帮助你。
试试这个而比较日期应为iso格式“yyyy-MM-dd”如果要仅比较日期,请使用此datehelper
<a href="https://plnkr.co/edit/9N8ZcC?p=preview"> Live Demo</a>
什么格式?
如果您构造了Javascript Date对象,您可以将它们减去以获得毫秒差(编辑:或只是比较它们):
js>t1 = new Date()
Thu Jan 29 2009 14:19:28 GMT-0500 (Eastern Standard Time)
js>t2 = new Date()
Thu Jan 29 2009 14:19:31 GMT-0500 (Eastern Standard Time)
js>t2-t1
2672
js>t3 = new Date('2009 Jan 1')
Thu Jan 01 2009 00:00:00 GMT-0500 (Eastern Standard Time)
js>t1-t3
2470768442
js>t1>t3
true
关系运算符<<=>>=可用于比较JavaScript日期:
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 2);
d1 < d2; // true
d1 <= d2; // true
d1 > d2; // false
d1 >= d2; // false
然而,等式运算符==!===!==无法用于比较(的值)日期,因为:
对于严格或抽象的比较,两个不同的对象永远不相等。只有当操作数引用同一对象时,比较对象的表达式才为真。
您可以使用以下任一方法比较相等的日期值:
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 1);
/*
* note: d1 == d2 returns false as described above
*/
d1.getTime() == d2.getTime(); // true
d1.valueOf() == d2.valueOf(); // true
Number(d1) == Number(d2); // true
+d1 == +d2; // true
Date.getTime()和Date.valueOf()都返回自1970年1月1日00:00UTC以来的毫秒数。Number函数和一元+运算符都在幕后调用valueOf()方法。