有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。


当前回答

假设您处理的是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

如您所见,我们去掉分隔符,然后比较整数。

其他回答

如果以下是您的日期格式,您可以使用此代码:

var first = '2012-11-21';
var second = '2012-11-03';
if(parseInt(first.replace(/-/g,""),10) > parseInt(second.replace(/-/g,""),10)){
   //...
}

将检查20121121号是否大于20121103号。

Date对象将执行您想要的操作-为每个日期构造一个,然后使用>、<、<=或>=对它们进行比较。

==,!=,==,和!==运算符要求您使用date.getTime(),如中所示

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

明确地说,直接检查日期对象是否相等是行不通的

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

不过,我建议您使用下拉列表或类似的受约束的日期输入形式,而不是文本框,以免您陷入输入验证地狱。


对于好奇的date.getTime()文档:

返回指定日期的数值,即自1970年1月1日00:00:00 UTC以来的毫秒数。(之前的时间返回负值。)

与往常一样比较<和>,但涉及==或==的任何内容都应使用+前缀。像这样:

const x=新日期(‘2013-05-23’);const y=新日期(‘2013-05-23’);//小于、大于即可:console.log('x<y',x<y);//假的console.log('x>y',x>y);//假的console.log('x<=y',x<=y);//真的console.log('x>=y',x>=y);//真的console.log('x===y',x===y);//假的,哎呀!//任何涉及“==”或“===”的内容都应使用“+”前缀//然后将比较日期的毫秒值console.log('+x===+y',+x===+y);//真的

使用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 date_today=new Date();
var formated_date = formatDate(date_today);//Calling formatDate Function

var input_date="2015/04/22 11:12 AM";

var currentDateTime = new Date(Date.parse(formated_date));
var inputDateTime   = new Date(Date.parse(input_date));

if (inputDateTime <= currentDateTime){
    //Do something...
}

function formatDate(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'PM' : 'AM';

    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    hours   = hours < 10 ? '0'+hours : hours ;

    minutes = minutes < 10 ? '0'+minutes : minutes;

    var strTime = hours+":"+minutes+ ' ' + ampm;
    return  date.getFullYear()+ "/" + ((date.getMonth()+1) < 10 ? "0"+(date.getMonth()+1) :
    (date.getMonth()+1) ) + "/" + (date.getDate() < 10 ? "0"+date.getDate() :
    date.getDate()) + " " + strTime;
}