2

I have this code that I only want to appear on a certain date. Which this code works just fine in Firefox, Chrome, and even safari. The code however is not working on IE. And I have no clue why.

I found that .toLocaleString() in IE separated them with a space instead of a comma.

  function givingTuesdayCode(){
     var isIE = /*@cc_on!@*/false || !!document.documentMode;
     var now = calcTime(-6);
     var splitnow = "";
     if ( isIE ){ splitnow = now.split(" "); }
     else{ splitnow = now.split(","); }

     if (splitnow[0] == "12/2/2014"){

        $('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>"); 
            $('.introrotation').css({'height': '265px'
                });
            $('.toggleButton').css({'display': 'none'
                });
    }

  function calcTime(offset){
     var date = new Date();
     var utc = date.getTime()+(360*60000);
     var nd = new Date(utc+(3600000*offset));
     return nd.toLocaleString();
  }
pormus
  • 118
  • 8
  • 1
    What is the value you are getting and what is expected? – ArinCool Dec 04 '14 at 17:58
  • Why not just use the `.toLocaleDateString` method instead? Also, if you're going to use a *locale string* you should mention which locale you'd like to use. "12/2/2014" is what you'll get with `en-US`, but something like `en-GB` will return "02/12/2014". It's probably wiser to avoid comparing dates as strings. – Sampson Dec 04 '14 at 19:20

1 Answers1

2

Instead of trying to match the date to a specific string (which might break in other locales), just compare the specific dates directly:

// Reset the hours, minutes, etc. so that comparison works
var today = (new Date()).setHours(0, 0, 0, 0);

// Month is zero-indexed (i.e. 0 = Jan, 11 = Dec)
var specificDate = (new Date(2014, 11, 2)).getTime();

if (today === specificDate) {
    $('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>"); 
    $('.introrotation').css({'height': '265px'});
    $('.toggleButton').css({'display': 'none'});
}

See Compare two dates with JavaScript for more information about comparing dates.

Ideally, you should be doing this on the server-side, not on the client-side, so that your message still shows up for browsers with JavaScript disabled.

Community
  • 1
  • 1
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63