1

I need to calculate if a date in the table is within 5 days and update the class of the parent row. I was able to do this (in FF and Chrome) but for some reason, it isn't working in IE8.

An item in the table would have a date like "08/28/13" and the TD would have the class "time".

$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
    var date = $(this).text();
    //sep date
    var year = date.substring(6, 10);
    var month = date.substring(0, 2);
    var day = date.substring(3, 5);
    alert(month + ' ' + day + ', ' + year);
    //today
    var d = new Date();
    var today = d.getDate();

    var m = new Array();
    m[0] = "01";
    m[1] = "02";
    m[2] = "03";
    m[3] = "04";
    m[4] = "05";
    m[5] = "06";
    m[6] = "07";
    m[7] = "08";
    m[8] = "09";
    m[9] = "10";
    m[10] = "11";
    m[11] = "12";
    var b = d.getMonth();
    var n = parseInt(m[d.getMonth()]);

    var monthStart = new Date(year, b, 1);
    var monthEnd = new Date(year, b + 1, 1);
    var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);

    var nextMonth = n + 1;
    var dayDiff = 5 - (monthLength - today);
    if ((monthLength - 5) < today && nextMonth == month && day < dayDiff) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
    if (n == month && today < day && day <= (today + 5)) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
});
Arturs
  • 1,258
  • 5
  • 21
  • 28
Shevy
  • 310
  • 4
  • 13
  • Do you have an error in IE8 ? – Denys Séguret Aug 26 '13 at 19:26
  • Try to 'use strict' in IE7-8 and see where your code has issues. – Arthur Kovacs Aug 26 '13 at 19:27
  • http://stackoverflow.com/questions/492994/compare-dates-with-javascript should give you the compare part you need. other than that just get the string MM/dd/yyyy and pass it into `new Date('MM/dd/yyyy')`, verify compatibility mode isn't on in IE (F12) then look on the header for IE Mode – abc123 Aug 26 '13 at 19:29
  • @dystroy None at all. – Shevy Aug 26 '13 at 19:29
  • Debug the code using IE Developer tools (F12) and then Script and pick the file you want to debug, set a breakpoint and start debugging. Then you can see where it is failing and why. In IE9 set the browser mode to IE8 if that's what you need to test. – abc123 Aug 26 '13 at 19:30
  • thanks will try to get an error and report back. – Shevy Aug 26 '13 at 19:46
  • @abc123 I tried it and saw no errors. Additionally, I checked out the question you linked and applied that method. However, I am having the same issue. It's working in FF and Chrome and not in IE8. – Shevy Aug 27 '13 at 12:22
  • So, IE says the type for the timestamp variables is 'number' but the values are returned as NaN. Only happening in IE. http://jsfiddle.net/shavonn/hZSyg/6/ – Shevy Aug 27 '13 at 13:05

1 Answers1

1

I tried the solution in the question that @abc123 commented and got the numerical timestamp- based difference between 5 days. I also updated the way I got today's date. Less code.

$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
    var date = $(this).text();
    var oppTime = date.substring(0, 10);

    var d = new Date();
    var today = d.toJSON();
    var t = today.substring(0, 4) + "/" + today.substring(5, 7) + "/" + today.substring(8, 10);

    var timeStamp_oppDate = new Date(oppTime).getTime();
    var timeStamp_thisDay = new Date(t).getTime();
    var timeDiff = timeStamp_oppDate - timeStamp_thisDay;

    if (432000000 >= timeDiff) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
});
Arturs
  • 1,258
  • 5
  • 21
  • 28
Shevy
  • 310
  • 4
  • 13