getTime()

Returns in milliseconds that is passed from January 1, 1970
We have to use this along with one date object.
Here is an example
<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>

Calculating Process time or lapse time

By using gettime() we can find out time taken by any calculation or any other process by finding the difference in milliseconds from start and end of the process.

Time taken for completing a process by using getTime()

This is a three step program.
First we will create the current date object and store the getTime() value in milliseconds in a variable.
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()


Here is the complete code.
<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>

Getting month part from date object
Date Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer