如何用 Javascript 进行日期比较?

我想用 javascript 比较两个日期。我一直在做一些研究,但我所能找到的是如何返回当前日期。我想比较两个不同的日期,与今天无关。我该怎么做。

var startDate = Date(document.form1.Textbox2);
381086 次浏览
if (date1.getTime() > date2.getTime()) {
alert("The first date is after the second date!");
}

对 Date 对象的引用

You can use the getTime() method on a Date object to get the timestamp (in milliseconds) relative to January 1, 1970. If you convert your two dates into integer timestamps, you can then compare the difference by subtracting them. The result will be in milliseconds so you just divide by 1000 for seconds, then 60 for minutes, etc.

JavaScript 的日期可以使用与其他数据类型使用的相同的比较运算符进行比较: > 、 < 、 < = 、 > = 、 = = !=,===,!==.

如果你有两个日期 A 和 B,那么 A < B,如果 A 比 B 更早回到过去。

但听起来你的问题是把一根绳子变成一次约会。您可以通过简单地将字符串作为一个参数传递给一个新的 Date:

var someDate = new Date("12/03/2008");

或者,如果您想要的字符串是表单字段的值,看起来可能是:

var someDate = new Date(document.form1.Textbox2.value);

如果该字符串不能被 JavaScript 识别为日期,那么仍然会得到一个 Date 对象,但它将是“无效的”。任何与其他日期的比较将返回 false。当转换为字符串时,它将成为“无效日期”。它的 getTime ()函数将返回 NaN,对日期本身调用 isNaN ()将返回 true; 这是检查字符串是否为有效日期的简单方法。

我宁愿使用 Date valueOf 方法而不是 = = = 或! = =

似乎 = = 正在比较内部 Object 的引用,而没有关于日期的内容。

You could try adding the following script code to implement this:

if(CompareDates(smallDate,largeDate,'-') == 0) {
alert('Selected date must be current date or previous date!');
return false;
}


function CompareDates(smallDate,largeDate,separator) {
var smallDateArr = Array();
var largeDateArr = Array();
smallDateArr = smallDate.split(separator);
largeDateArr = largeDate.split(separator);
var smallDt = smallDateArr[0];
var smallMt = smallDateArr[1];
var smallYr = smallDateArr[2];
var largeDt = largeDateArr[0];
var largeMt = largeDateArr[1];
var largeYr = largeDateArr[2];


if(smallYr>largeYr)
return 0;
else if(smallYr<=largeYr && smallMt>largeMt)
return 0;
else if(smallYr<=largeYr && smallMt==largeMt && smallDt>largeDt)
return 0;
else
return 1;
}
new Date('1945/05/09').valueOf() < new Date('2011/05/09').valueOf()

You can try this code for checking which date value is the highest from two dates with a format MM/DD/YYYY:

function d_check() {
var dl_sdt=document.getElementIdBy("date_input_Id1").value; //date one
var dl_endt=document.getElementIdBy("date_input_Id2").value; //date two


if((dl_sdt.substr(6,4)) > (dl_endt.substr(6,4))) {
alert("first date is greater");
return false;
}


else if((((dl_sdt.substr(0,2)) > (dl_endt.
substr(0,2)))&&(frdt(dl_sdt.substr(3,2)) > (dl_endt.substr(3,2))))||
(((dl_sdt.substr(0,2)) > (dl_endt.substr(0,2)))&&
((dl_sdt.substr(3,2)) < (dl_endt.substr(3,2))))||
(((dl_sdt.substr(0,2)) == (dl_endt.substr(0,2)))&&((dl_sdt.substr(3,2)) >
(dl_endt.substr(3,2))))) {
alert("first date is greater");
return false;
}


alert("second date is digher");
return true;
}

/*for checking this....create a form and give id's to two date inputs.The date format should be mm/dd/yyyy or mm-dd-yyyy or mm:dd:yyyy or mm.dd.yyyy like this. */

function fn_DateCompare(DateA, DateB) {     // this function is good for dates > 01/01/1970


var a = new Date(DateA);
var b = new Date(DateB);


var msDateA = Date.UTC(a.getFullYear(), a.getMonth()+1, a.getDate());
var msDateB = Date.UTC(b.getFullYear(), b.getMonth()+1, b.getDate());


if (parseFloat(msDateA) < parseFloat(msDateB))
return -1;  // lt
else if (parseFloat(msDateA) == parseFloat(msDateB))
return 0;  // eq
else if (parseFloat(msDateA) > parseFloat(msDateB))
return 1;  // gt
else
return null;  // error
}
    function validateform()
{
if (trimAll(document.getElementById("<%=txtFromDate.ClientID %>").value) != "") {
if (!isDate(trimAll(document.getElementById("<%=txtFromDate.ClientID %>").value)))
msg = msg + "<li>Please enter valid From Date in mm/dd/yyyy format\n";
else {
var toDate = new Date();
var txtdate = document.getElementById("<%=txtFromDate.ClientID %>").value;
var d1 = new Date(txtdate)
if (Date.parse(txtdate) > Date.parse(toDate)) {
msg = msg + "<li>From date must be less than or equal to today's date\n";
}
}
}


if (trimAll(document.getElementById("<%=txtToDate.ClientID %>").value) != "") {
if (!isDate(trimAll(document.getElementById("<%=txtToDate.ClientID %>").value)))
msg = msg + "<li>Please enter valid To Date in mm/dd/yyyy format\n";
else {
var toDate = new Date();
var txtdate = document.getElementById("<%=txtToDate.ClientID %>").value;
var d1 = new Date(txtdate)


if (Date.parse(txtdate) > Date.parse(toDate)) {
msg = msg + "<li>To date must be less than or equal to today's date\n";
}
}
}