简体   繁体   中英

JS isn't working correctly on IE but works perfect in Chrome

I'm trying to create a JS that print out today's date, it's working perfectly with Chrome but it's not on Internet Explorer.

I'm not sure what JS function is not available on Internet Explorer? My facility is still default to Internet Explorer for some reasons and it's extremely frustrating trying to find a work around.

//get today's date & month
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const date = today.getDate();

//get day of the week
  var weekday = new Array(7);
  weekday[0] = "Sunday";
  weekday[1] = "Monday";
  weekday[2] = "Tuesday";
  weekday[3] = "Wednesday";
  weekday[4] = "Thursday";
  weekday[5] = "Friday";
  weekday[6] = "Saturday";
  var dayOfWeek = weekday[today.getDay()];
const todayDate = month+ "/"+ date + "/" + year;
document.querySelector(".dayOfWeek").innerHTML= dayOfWeek ;
document.querySelector(".sdDate").innerHTML= todayDate;

//get present's month
var monthOfYear = new Array(12);
  monthOfYear[1] = "January";
  monthOfYear[2] = "February";
  monthOfYear[3] = "March";
  monthOfYear[4] = "April";
  monthOfYear[5] = "May";
  monthOfYear[6] = "June";
  monthOfYear[7] = "July";
  monthOfYear[8] = "August";
  monthOfYear[9] = "September";
  monthOfYear[10] = "October";
  monthOfYear[11] = "November";
  monthOfYear[12] = "December";
  var monthOfYearprt = monthOfYear[month];
  document.querySelector(".bdMonth").innerHTML= monthOfYearprt;

  
<div id="specialDay">
            <div class="todayDate">
              <span class="dayOfWeek"></span>
              <br>
              <span class="sdDate"></span>
            </div>
           
          
        </div>

Try declaring your vars with var instead of const .

As it stands, const is not widely supported by IE .

Basically, if you're testing IE outside of IE11 then your code may not work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM