4

I'm facing some unusual issue.

var past = utils.stringToDate(past, 'dd-mm-yyyy', '-');
var today = new Date();
past.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);

return today > past? true: false;

Above code is used to set a flag and this flag is used to decide the flow a user is. Now issue is, its working in most browsers including IE, but fails in Safari on windows (Working fine in Safari, Mac) and Opera.

past is a date that I receive from the server and the value is 27-09-2015.

stringToDate is a function that formats date in specified format.

Test Case

past : 27-09-2015
today: 25-09-2015

Still above code returns true in the mentioned browsers.

So the question is, is there a difference in browsers in comparing date objects in javascript and if yes, what all less known cases should I be aware of?

Also this variable is set only once in entire life cycle and is not updated anywhere else.

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • 7
    "fails in Safari on windows" — Safari for Windows has been unsupported for years. In 2012 there are over a hundred and twenty known security problems with it and there hasn't been a release since. I'd avoid putting effort into making things work on it. Let it die. https://en.wikipedia.org/wiki/Safari_(web_browser)#Security_updates_for_Snow_Leopard_and_Windows_platforms – Quentin Sep 25 '15 at 13:57
  • There are known support issues with date on Safari: http://stackoverflow.com/questions/4310953/invalid-date-in-safari – saeraphin Sep 25 '15 at 13:58
  • How could it give `true` if your `past` is bigger than your `today`? – smnbbrv Sep 25 '15 at 13:58
  • @smnbbrv That is the dark area of my knowledge. That is why I seek help. – Rajesh Sep 25 '15 at 14:03
  • What actually happens if you compare two objects by '<' or '>' ist that the valueOf method of the objects is called. For date valueOf should return the unixtimestamp. Try to do this manually (today.getTime() > past.getTime() ) to imitate this behaviour. If the problem still occurs its maybe another problem. – TobiSH Sep 25 '15 at 14:03
  • @Quentin Thanks for the information, but there is a user who is using my product. Now if there exists a hack that can make things work, I would do it. – Rajesh Sep 25 '15 at 14:05
  • I can not reproduce your problem on Opera 31 on MacOs 10.10. Can you be a bit more specific which opera version doesn't work for you? Can you furthermore provide the _utils.stringToDate_ function? – TobiSH Sep 25 '15 at 14:12
  • @TobiSH Even I'm checking for it. A user reported similar issue in opera, but even I cannot produce it. Have tested on v.31, v.24, v.17 Must be some corner case. Will update once I get proper issue for Opera. – Rajesh Sep 25 '15 at 14:19
  • There could be a problem in stringToDate. Can you add the code of that function? – Rodrigo5244 Oct 10 '15 at 14:54
  • @Waterscroll If there was issue in code then it would fail consistently on all browser and not specifically in Safari in Windows. And stringToDate just accepts date in *dd-mm-yyyy*, splits it and returns a date object. – Rajesh Oct 10 '15 at 15:07
  • If you pass a string to Date, the behaviour depends on the browser. So I wanted to make sure that this is not the case. – Rodrigo5244 Oct 10 '15 at 15:24

1 Answers1

0

You could compare them adding a function to the prototype like this:

/*
 * Compares two Date objects numerically.
 *
 * @param date the Date to be compared.
 * @return the value of 0 if this Date is equal to the argument date; 
 * a value of -1 if this Date is numerically less than the argument date;
 * and a value of 1 if this Date is numerically greater than the argument date.
 */
Date.prototype.compareTo = function(date) {
    var d1_this = this.getTime(),
        d2_time = date.getTime();

    if (d1_this == d2_time) {
        return 0;
    } else if (d1_this < d2_time) {
        return -1;
    } else {
        return 1;
    }
}

Then use it like:

var date1 = new Date(), date2 = new Date(); // init vars
date1.compareTo(date2); // or
date2.compareTo(date1);
Chicodelarose
  • 827
  • 7
  • 22
  • 1
    You can update your function to `return d1_this - d2_this`. Also as Quintin has already suggested, it's an issue with safari on Windows. We had discontinued it. Still thanks for advice. – Rajesh May 08 '16 at 03:18