1

I have made this with js:

// time

var date = new Date();

var currentTime = date.getHours() + ':' + date.getMinutes();

document.getElementById('timex').value = currentTime;

This script inserts the hour and minutes in a input.

There is a problem, at 16:06 the time becomes 16:6. And this causes a problem with the html input. The same thing happens at 1am. Become 1:34 and not 01:34.

Segala Mirco
  • 69
  • 1
  • 5

2 Answers2

2

Try this, simple and works like a charm:

var date = new Date();

var currentTime = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);

document.getElementById('timex').value = currentTime;
Matansh
  • 764
  • 4
  • 13
0
d.toISOString().substring(11).slice(0,5)
raven
  • 2,381
  • 2
  • 20
  • 44