<script type="text/javascript">
var my_time = new Date(); // date object
document.write(my_time.getTime()); // Output
</script>
Output is here
Using a button to display getTime() of current date object.
Source of this demo is here
<script type="text/javascript">
function show_gettime(){
var my_time = new Date(); // date object
//document.write(my_time.getTime()); // Output
document.getElementById('display').innerHTML = my_time.getTime();
}
</script>
<input type=button onclick='show_gettime()' value='Show';>
<div id='display'></div>
var my_time1 = new Date(); // date object
my_time1=my_time1.getTime(); // first time variable
Then we will keep the system busy by creating a loop and create some time delay.
for(i=1; i<=100000000; i++){
if((i % 10000) == 0){
document.getElementById('display1').innerHTML=i;
}
}
Third step is to create date object and read the getTime() method again.
var my_time2 = new Date(); // date object
my_time2=my_time2.getTime(); // second time variable
The difference in first getTime value and present getTime value will give us the duration the system has taken to complete the looping process.
var diff = ( my_time2-my_time1); // difference in time
document.getElementById('display2').innerHTML = 'The process took ' + diff + ' Milliseconds';
DEMO : Process time by using getTime()
<html>
<head>
<title>Demo of getTime function</title>
</head>
<body>
<script type="text/javascript">
function show_gettime(){
var my_time1 = new Date(); // date object
my_time1=my_time1.getTime(); // first time variable
for(i=1; i<=100000000; i++){
if((i % 10000) == 0){
document.getElementById('display1').innerHTML=i;
}
}
var my_time2 = new Date(); // date object
my_time2=my_time2.getTime(); // second time variable
var diff = ( my_time2-my_time1); // difference in time
document.getElementById('display2').innerHTML = 'The process took ' + diff + ' Milliseconds';
}
</script>
<input type=button onclick='show_gettime()' value='Start';>
<div id='display1'></div>
<br><br>
<div id='display2'></div>
</body>
</html>