0

Hi i have an issue with ie not displaying my javascript, im a little puzzled as it works fine in google chrome., i have tried f12 debugging and enabled javascript but not with any luck so any help would be much appreciated. In chrome it shows correct amount of days and in ie it just says NaN.

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countup(yr,m,d){
 var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy
var paststring=montharray[m-1]+" "+d+", "+yr
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)
difference+=" days"

document.write("<span style=\"color:#A4A4A4;font-size: 3em; \"> It\'s been  "+difference+" since the last incident.<\/span>")

}
//enter the count up date using the format year/month/day

countup('<?php echo $date1; ?>')
MFC86
  • 1
  • 1
  • Welcome to web development. – vol7ron Feb 27 '16 at 00:31
  • @Virtyaluk poses a valid question: what is being returned by *$date1* also, which IE? It works fine in Edge, unless your value is as the comment suggests (like in the format `'2016/02/25'`) which returns *NAN days* – vol7ron Feb 27 '16 at 00:39
  • Very strange to take a known date object...turn it into string, to turn around and then use Date.parse() on that string – charlietfl Feb 27 '16 at 00:44
  • Well, I repeat, what actually `` returns? – virtyaluk Feb 28 '16 at 02:36
  • Hi sorry for the late reply iv been away working, the $date1 returns a mysql date, will be 2016/02/25 but i have tried changing the date to a different format with no luck – MFC86 Mar 03 '16 at 09:11
  • $date1=$row['date_of_incident']; – MFC86 Mar 03 '16 at 09:20

2 Answers2

1

What actually <?php echo $date1; ?> returns? Because it seems like countup expects 3 arguments but takes only 1.

virtyaluk
  • 145
  • 1
  • 12
0

If your JavaScript comment is correct, you're passing the value as 'YYYY/MM/DD', which is not the format your function expects. Your function expects three different numbers/strings countup('02','25','2016'), not countup('2016/02/25')

The error occurs at your var paststring = montharray[m-1]+" "+d+", "+yr. This will result as undefined undefined, 2016/02/25, which is not a valid date.

The reason it works in Chrome is because Date.parse('undefined undefined, 2016/02/25') can actually produce the date, whereas other browsers (including Firefox) do the valiant thing and fail.

See this answer and this answer for a little more information.


To fix your code, choose one:

  • adjust your function to receive the input as you want to send it
  • adjust your function call to send the proper arguments as defined by your function parameters
  • use another library, like moment.js to do all the calculations -- it's one of the best date management libraries available for JS
Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • Thanks for your comment, the echo date is coming from mysql so im guessing its the wrong format, i have tried different formats but its still saying Nan days, – MFC86 Mar 03 '16 at 09:12
  • $date1=$row['date_of_incident']; – MFC86 Mar 03 '16 at 09:17
  • @MFC change your function to only have one date parameter. You don't need to break it into day, month, and year because you're not breaking it up. – vol7ron Mar 03 '16 at 12:40