1

I have script which works well in Chrome and Firefox, but when tried same with Safari version 5.1.7 which could be latest for windows showing NAN values can anybody help me out to solve this problem I tried of parseInt but it didn't work.

here is my code;

var date1 = new Date($('#arrival').val());
var  date2 = new Date($('#departure').val());
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
   //parseInt("0"+$(this).html(),10) 
diffDays=parseInt(diffDays);
console.log('Selected diffDays: '+diffDays);

the comment part of parseint shows me 0's.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 4
    Given the lack of any PHP in your problem code, why have you tagged this question PHP? – Mark Baker Dec 23 '13 at 14:30
  • 3
    Does this answer your question? [Invalid date in safari](https://stackoverflow.com/questions/4310953/invalid-date-in-safari) – m13r Mar 05 '21 at 16:56

1 Answers1

4

The most likely answer is that the format of the dates you're reading from the #arrival and #departure fields isn't supported by Safari. JavaScript only has one date string format that is required in compliant implementations, and that's fairly recent (~three years ago, as of ES5). (That said, I've never met a browser that didn't support yyyy/MM/dd, but the timezone of the resulting date varies.)

If you feed an invalid string into new Date(), you get an invalid date. The getTime method returns NaN with invalid dates, and then of course that propagates throughout your calculation (as the result of all math operators is NaN if any operand is NaN).

The solution is to ensure that the strings are in the supported format, or parse them yourself. There are also libraries to help, such a MomentJS.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • i'm passing for #arrival has in this format =date('Y-m-d',strtotime($this->start))?> which on console.log showing invalid date which format of date shall take – user3129830 Dec 23 '13 at 14:41
  • @user3129830: Sadly, it looks like that version of Safari doesn't yet support the ES5 standard date format; I just tried it [here](http://jsbin.com/uyEmEla/1). If you use slashes instead ([example](http://jsbin.com/uyEmEla/2)) it'll probably start working; that format (`yyyy/MM/dd`) is not in the spec but is very widely supported. Beware of timezone issues. Or, again, use a library. – T.J. Crowder Dec 23 '13 at 15:03