有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
假设你得到了日期对象A和B,得到它们的EPOC时间值,然后减去以毫秒为单位的差值。
var diff = +A - +B;
这就是全部。
其他回答
你可以用最简单易懂的方式进行比较。
<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>
当然,这段代码会帮助你。
为了在众多现有选项中添加另一种可能性,您可以尝试:
if (date1.valueOf()==date2.valueOf()) .....
…这似乎对我有用。当然,你必须确保这两个日期都没有定义。。。
if ((date1?date1.valueOf():0)==(date2?date2.valueOf():0) .....
通过这种方式,我们可以确保如果两者都未定义,或者。。。
if ((date1?date1.valueOf():0)==(date2?date2.valueOf():-1) .....
…如果你希望他们不平等。
假设您处理的是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
如您所见,我们去掉分隔符,然后比较整数。
使用momentjs进行日期操作。
使用
isSameOrAfter()方法
moment('2010-10-20').isSameOrAfter('2010-10-20') //true;
使用isAfter()方法检查一个日期是否在另一个日期之后
moment('2020-01-20').isAfter('2020-01-21'); // false
moment('2020-01-20').isAfter('2020-01-19'); // true
使用isBefore()方法检查一个日期是否早于另一个日期。
moment('2020-01-20').isBefore('2020-01-21'); // true
moment('2020-01-20').isBefore('2020-01-19'); // false
使用isSame()方法检查一个日期与另一个日期是否相同
moment('2020-01-20').isSame('2020-01-21'); // false
moment('2020-01-20').isSame('2020-01-20'); // true
嗨,这是我比较日期的代码。在我的情况下,我正在检查是否允许选择过去的日期。
var myPickupDate = <pick up date> ;
var isPastPickupDateSelected = false;
var currentDate = new Date();
if(currentDate.getFullYear() <= myPickupDate.getFullYear()){
if(currentDate.getMonth()+1 <= myPickupDate.getMonth()+1 || currentDate.getFullYear() < myPickupDate.getFullYear()){
if(currentDate.getDate() <= myPickupDate.getDate() || currentDate.getMonth()+1 < myPickupDate.getMonth()+1 || currentDate.getFullYear() < myPickupDate.getFullYear()){
isPastPickupDateSelected = false;
return;
}
}
}
console.log("cannot select past pickup date");
isPastPickupDateSelected = true;
推荐文章
- 同步和异步编程(在node.js中)的区别是什么?
- 在d3.js中调整窗口大小时调整svg的大小
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 单元测试:日期时间。现在
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标