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.

3
  • 1
    Check if the value is less than 10, if so, insert a preceding zero. Commented Apr 2, 2017 at 16:03
  • 1
    PLEASE search google first next time you want to ask. This is not just a FAQ it is a FFAQ Commented Apr 2, 2017 at 16:05
  • possible duplicate stackoverflow.com/a/6040556/7549867 Commented Apr 2, 2017 at 16:06

2 Answers 2

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

If you are satisfied with the answer, I would really appreciate it if you accept the answer :)
0
d.toISOString().substring(11).slice(0,5) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.