0

I am trying to add 30 minutes in time but it doesn't display correctly, please help me. Where is the mistake in my code?

The minutes display like a garbage value...

  
<div id="time"></div>

<script>
function checkTime(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function startTime() {
  var today = new Date();
  var h = today.getHours();
   
 var m = today.setMinutes(today.getMinutes()+30);//**here I am adding plus 30 mint but not showing correct**
 
  var s = today.getSeconds();
  // add a zero in front of numbers<10
  m = checkTime(m);
  s = checkTime(s);

//Minutes display like garbage value.... 

  document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
  t = setTimeout(function() {
    startTime()
  }, 500);
}
startTime();
</script>

   

Please review my code and correct it.

vrugtehagel
  • 1,009
  • 1
  • 9
  • 20
Forapp T
  • 1
  • 1

1 Answers1

-1

You're nesting two different functions, I don't know why.

This line:

var m = today.setMinutes(today.getMinutes()+30);

Should be:

var m = today.getMinutes()+30

If you run the code snippets it works.

  
<div id="time"></div>

<script>
function checkTime(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function startTime() {
  var today = new Date();
  var h = today.getHours();
  
  // CORRECTION HERE
  var m = today.getMinutes()+30;
 
  var s = today.getSeconds();
  // add a zero in front of numbers<10
  m = checkTime(m);
  s = checkTime(s);

//Minutes display like garbage value.... 

  document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
  t = setTimeout(function() {
    startTime()
  }, 500);
}
startTime();
</script>

   
GrafiCode
  • 3,307
  • 3
  • 26
  • 31