0

In my JavaScript code I have to variables (x and y) with datetime values. All of the following statements return false:

  • x < y -> false
  • x > y -> false
  • x == y -> false
  • x === y -> false

When I debug the code both x and y have the same value 'Sat Jan 1 12:00:00 UTC+0100 2011' and the same type.

I am using IE9.

Is there a different way to check if two datetime values are equal?

update (code) - I tried to extract some relevant parts, whole project is very big:

var data;


//Dates are loaded from a webservice and returned as strings; they are parsed with the following function


data = parseD(getDataFromService());

function parseD (data) {
    for (var i = 0; i < data.length; i++) {
        var d = data[i];
        for (var key in d) {
            if (d[key] && d[key].toString().substring(0, 6) === "/Date\(")
                d[key] = new Date(parseInt(d[key].toString().substr(6)));
        }
    }
    return data;
};

function getCssForCell(row, columnID, dataContext, value) {
            var css = "";
            if (columnID === "VF" && value) {
                if (row > 0) {
                    if (data[row].VF < addDays(data[row - 1].VF, 2)) {
                        css = "error_cell";
                    }
                }
            }
            if (columnID === "VU" || columnID === "VF") {
                if (dataContext.VF && dataContext.VU) {
                    if (dataContext.VU <= dataContext.VF) {
                        css = "error_cell";
                    }
                }
            }
            if (columnID === "Rate" && row < (strict_mode ? data.length - 1 : data.length - 2)) { //value for Rate must be assigned
                if (value === undefined || value === null) {
                    css = "error_cell";
                }
            }
            if (columnID === "VF" && row < (strict_mode ? data.length - 1 : data.length - 2)) {
                if (value === undefined || value === null) {
                    css = "error_cell";
                }
            }

            if (columnID === "Rate" && value < 0) {
                css = "error_cell";
            }

            //PROBLEM IN NEXT LINE
            if (columnID === "VU" && row < data.length - 1 && dataContext.VU && data[row + 1] && addDays(dataContext.VU, 1) != data[row + 1].VF ) {
                css = "error_cell";
            }

            return css;
        }

function addDays(date, days) {
    if (date) {
        var result = new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours());
        result.setDate(result.getDate() + days);
        return result;
    }
    return date;
}
1
  • Comparing dates should work if they are actual date objects. Otherwise, try to compare the UTC value of both. Commented Aug 4, 2012 at 15:17

1 Answer 1

1

You should not compare Date object instances, but their values, compare using .getTime() method instead:

x.getTime() < y.getTime();
x.getTime() > y.getTime();
x.getTime() == y.getTime();
x.getTime() === y.getTime();

.getTime() returns the numeric value corresponding to the time for the specified date according to universal time, this value is the number of milliseconds since 1 January 1970 00:00:00 UTC, also it is equivalent to .valueOf() method.

Sign up to request clarification or add additional context in comments.

2 Comments

I already used x < y and x > y to compare dates (without getTime) and it worked as expected. Is it really the normal behavior in JavaScript that dateA == dateB is always false?
for ">" and "<" it will work, but for "==" - no, it is recommended to use .getTime() or .valueOf() to work with Date's numeric value, you will not be able to know if value of the Date instances are equal, only with dirty code like this one: if (x > y) { /* ... */ } else if (x < y) { /* ... */ } else { /* they are not equal */ }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.