简体   繁体   中英

How can I display values of an array in an inline command without the “,” in JavaScript?

I have a JavaScript where I created the 'z' array, which is the current hour and minute. I would like to print 'z' onclick, but not in an array format.

If the current time is 2:18, the output of the code below is: 2,18. What I would like is 2:18.

Thank you for your consideration and answer!

<script>
  var today = new Date();  
  h = today.getHours();  
  m = today.getMinutes();  
  z = [h,m];  
</script>  

<button onclick="document.getElementById('demo').innerHTML=z">time</button>
<p id="demo"></p>

While there are many better ways to do this, you can use: z.join(':')

I would check out http://momentjs.com/

document.getElementById('demo').innerHTML=z[0]+':'+z[1];

Change z to:

z = h + ":" + m;

It will display as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM