show time in javascript code example
Example 1: get current time in js
<script>
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
</script>
Example 2: calculate today date javascript
var today = new Date();
Example 3: get time in javascript
<a onclick="timeNow(test1)" href="#">SET TIME</a>
function timeNow(i) {
i.value = new Date()
}
Example 4: display time in html using javascript
var month=x.getMonth()+1;
var day=x.getDate();
var year=x.getFullYear();
if (month <10 ){month='0' + month;}
if (day <10 ){day='0' + day;}
var x3= month+'-'+day+'-'+year;
var hour=x.getHours();
var minute=x.getMinutes();
var second=x.getSeconds();
if(hour <10 ){hour='0'+hour;}
if(minute <10 ) {minute='0' + minute; }
var x3 = x3 + ' ' + hour+':'+minute+':'+second
<span id='ct4' style="background-color:#FFFF00"></span>
Example 5: display time in html using javascript
var month=x.getMonth()+1;
var day=x.getDate();
var year=x.getFullYear();
if (month <10 ){month='0' + month;}
if (day <10 ){day='0' + day;}
var x3= month+'-'+day+'-'+year;
var hour=x.getHours();
var minute=x.getMinutes();
if(hour <10 ){hour='0'+hour;}
if(minute <10 ) {minute='0' + minute; }
var x3 = x3 + ' ' + hour+':'+minute+'
<span id='ct4' style="background-color:#FFFF00"></span>