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


当前回答

我通常将日期作为时间戳(数字)存储在数据库中。

当我需要比较时,我只需在这些时间戳或

将其转换为Date Object,然后根据需要与><进行比较。

请注意,==或==无法正常工作,除非变量是同一日期对象的引用。

首先将这些Date对象转换为时间戳(数字),然后比较它们的相等性。


日期到时间戳

var timestamp_1970 = new Date(0).getTime(); // 1970-01-01 00:00:00
var timestamp = new Date().getTime(); // Current Timestamp

截至日期的时间戳

var timestamp = 0; // 1970-01-01 00:00:00
var DateObject = new Date(timestamp);

其他回答

您可以使用以下代码:

var firstValue = "2012-05-12".split('-');
var secondValue = "2014-07-12".split('-');

 var firstDate=new Date();
 firstDate.setFullYear(firstValue[0],(firstValue[1] - 1 ),firstValue[2]);

 var secondDate=new Date();
 secondDate.setFullYear(secondValue[0],(secondValue[1] - 1 ),secondValue[2]);     

  if (firstDate > secondDate)
  {
   alert("First Date  is greater than Second Date");
  }
 else
  {
    alert("Second Date  is greater than First Date");
  }

还要检查Date类的MDN文章。

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以来的毫秒数。(之前的时间返回负值。)

function compare_date(date1, date2){
const x = new Date(date1)
const y = new Date(date2)
function checkyear(x, y){
    if(x.getFullYear()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getFullYear()<y.getFullYear()){
        return "Date2 > Date1"
    }
    else{
        return checkmonth(x, y)
    }
}
function checkmonth(x, y){
    if(x.getMonth()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getMonth()<y.getMonth){
        return "Date2 > Date1"
    }
    else {
        return checkDate(x, y)
    }
}
function checkDate(x, y){
    if(x.getDate()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getDate()<y.getDate()){
        return "Date2 > Date1"
    }
    else {
        return checkhour(x,y)
    }
}
function checkhour(x, y){
    if(x.getHours()>y.getHours()){
        return "Date1 > Date2"
    }
    else if(x.getHours()<y.getHours()){
        return "Date2 > Date1"
    }
    else {
        return checkhmin(x,y)
    }
}
function checkhmin(x,y){
    if(x.getMinutes()>y.getMinutes()){
        return "Date1 > Date2"
    }
    else if(x.getMinutes()<y.getMinutes()){
        return "Date2 > Date1"
    }
    else {
        return "Date1 = Date2"
    }
}
return checkyear(x, y)

为了在众多现有选项中添加另一种可能性,您可以尝试:

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

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