-2

I'm trying to separate concerns using modules in Js eveything is going Ok except that I want to display the time of the system and I did that too but when I output it I got an [object Object] string and what proves me right that really I got the right system Time is that when I console logged the interCtrl.getTheTime I got the time of the system and here is a screenshot:

enter image description here

here is my code to let you understand better:

JS:

var controller = (function(interCtrl, UICtrl) {

  //1.get the time
  setInterval(document.querySelector(".Time").innerHTML = interCtrl.getTime(),100);
  // console.log(interCtrl.getTime());
  //2.get the date
  document.querySelector(".subDate").innerHTML = interCtrl.date();

  };


  document.querySelector(".add__btn").addEventListener("click", ctrlAddPlans);

  document.addEventListener("keypress", function(e) {
    if (e.keyCode === 13) {
      ctrlAddPlans();
    }
  });

})(internalController);


var internalController = (function() {

  //some code
  function getTheDate() {
    var newDate;
    var d = new Date();
    var day = d.getDate();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    // var x=1;
    if (month.toString().length < 2) {
      month = " 0" + month + " ";
    }
    var date = " %11%/%04%/%2019%";
    newDate = date.replace("%11%", day + " ");
    newDate = newDate.replace("%04%", month);
    newDate = newDate.replace("%2019%", " " + year);
    return newDate;
  }

  function getTheTime() {
    //.get the timeDiv
    // const timeDiv =
    //.get the time (h,m,s)
    var date = new Date();
    var hours = date.getHours();
    if (hours.toString().length < 2) {
      hours = `0${hours}`;
    }
    var mins = date.getMinutes();
    if (mins.toString().length < 2) {
      mins = `0${mins}`;
    }
    var seconds = date.getSeconds();

    return {
      time: hours+ " : "+ mins+" : "+seconds
    };

  }



  return {
    date: function getDate() {
      return getTheDate();
    },
    getTime: function getTime() {
      return getTheTime();
    }
  };


})();

I saw this question but it didn't help me what does [object Object] mean?

so any help please and thank you in advance

Amine El were
  • 797
  • 2
  • 8
  • 21

1 Answers1

2

The output of the getTime function is an object. To make it output a string, you can change its return statement from

    return {
      time: hours+ " : "+ mins+" : "+seconds
    };

to

    return hours + " : " + mins+" : " + seconds;

Or, alternatively, if you want it to be an object, you'll have to access its time property to get the value. Like

var timeObj = interCtrl.getTheTime();
var timeString = timeObj.time;
mbojko
  • 13,503
  • 1
  • 16
  • 26