0

I wanna show the time when the user clicks on the button but it doesn't work. I used the Date() function.

function time() {
  document.getElementById("datebtn").innetHTML = getDate();
}
<p id="datebtn">time is =</p>

<button type="button" onclick="time()">click to show the time</button>
Nope
  • 22,147
  • 7
  • 47
  • 72
shamim
  • 47
  • 2
  • 6
  • don't use inline js - it's bad practice that leads to hard-to-maintain code – treyBake Apr 13 '18 at 15:40
  • did you search online for getDate and getDate examples? The answer to your problem is in the first example I found. If you are new to javascript check out w3schools.com – Jose Jimenez Apr 13 '18 at 15:41
  • 4
    @JoseJimenez don't recommend w3schools - it's full of bad practice and out-of-date code – treyBake Apr 13 '18 at 15:42
  • Thanks to those of you who corrected me about the bad referral. See documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate – Jose Jimenez Apr 13 '18 at 19:04
  • `var a = new Date(); (new Date(a.setMinutes(a.getMinutes() - a.getTimezoneOffset())).toISOString()).substr(11,5);` – RobG Apr 16 '18 at 04:54

3 Answers3

3

To access the current date, you use new Date() in JavaScript. However, you'd probably want to format it so you get hours:minutes:seconds. So we make a new date object and format it like so:

var currentdate = new Date(); 
var timenow = + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();

Also you have a typo: It's innerHTML not innetHTML. innerHTML removes the html inside (that is in this case all the html between <p id="datebtn"> and </p>) and replaces it with the text you give it. So you'd have to include the prefix text too if you want to use it:

document.getElementById("datebtn").innerHTML="time is ="+timenow;

Sow the final code becomes

function time() {
var currentdate = new Date(); 
var timenow = + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();
  document.getElementById("datebtn").innerHTML = "time is ="+timenow;
}
<p id="datebtn">time is =</p>

<button type="button" onclick="time()">click to show the time</button>
2

Hope this will help:

You have to use functions to get hour, minutes and seconds from date object.

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

function time() {
  var date = new Date();
  var hh = date.getHours();
  var mm = date.getMinutes();
  var ss = date.getSeconds();
  
  // adding 0 for single digits
  
  mm = checkTime(mm);
  ss = checkTime(ss);
  document.getElementById('datebtn').innerHTML = hh + ":" + mm + ":" + ss;
}
<p id="datebtn">time is =</p>

<button type="button" onclick="time()">click to show the time</button>
Ash
  • 473
  • 2
  • 10
-1

This allows you to get the current date and time and use it how you want, from alerts to mails..

   <SCRIPT>
        var now=new Date(); // neccessary
        var day=now.getDate(); // Day
        var month=now.getMonth()+1; // Month (+1 because january = 0)
        var year=now.getFullYear(); // Year
        var seconds = now.getSeconds(); // Seconds
        var minutes = now.getMinutes(); // Minutes
        var hour = now.getHours(); // Hours
        document.write("",day,"/",month,"/",year," - ",hour,":",minutes,":",seconds); // prints "13/04/2018 - 18:37:32"
    </SCRIPT>

EDIT: so as I got downvoted for helping, here's the best thing I can think of right now, because I clearly don't know more. it is the simpliest I can think of.

<p id="time">Getting the current time..</p>
<button onclick="showTime()">Show Time</button>
<script type="text/javascript">
    function showTime(){
        var now=new Date();
        var seconds = now.getSeconds();
        var minutes = now.getMinutes();
        var hour = now.getHours();
        document.getElementById('time').innerHTML = hour + ":" + minutes + ":" + seconds;
    }
</script>
  • Your answer uses `document.write` instead of OPs desired target output and it outputs the date instead of the time as OP requested. – Nope Apr 13 '18 at 16:06
  • well, I thought it could be usefull, as he didn't seem to know how to get the date properly. are you seriously putting -1 on everyone's answer @Nope ? – Antoine Petitot Apr 13 '18 at 16:25
  • @AntoinePetitot—if they aren't good answers, they should be. Time and date questions are frequently poorly answered. – RobG Apr 16 '18 at 05:00