getTime

Category:Control

getTime()

Category: Control

Gets the number of milliseconds elapsed since January 1, 1970.

Time on a computer is calculated from the number of milliseconds elapsed since January 1, 1970. Every computer has a counter that is counting these milliseconds. You can use getTime() to calculate the elapsed time between two events.

Examples


console.log(getTime());

How Fast? Calculate how long it takes to execute two statements. Use the speed slider in the Debug Console to slow down execution.

// Calculate how long it takes to execute two statements. Use the speed slider in the Debug Console to slow dowen execution.
var start=getTime();
var stop=getTime();
console.log(stop-start);

Five Clicks Calculate the elapsed time for 5 clicks.

// Calculate the elapsed time for 5 clicks.
var start=0;
var stop=0;
textLabel("instructions", "Click the button five times as fast as you can");
button("gameButton", "Click me!");
textLabel("results", "");
var count = 0;
onEvent("gameButton", "click", function(){
  if (count==0) start=getTime();
  count = count + 1;
  if (count==5) {
    stop=getTime();
    setText("results", "You clicked 5 times in " + (stop-start) + " milliseconds.");
  }
});

Syntax

getTime();

Parameters

getTime() does not take any parameters.

Returns

Returns the number of milliseconds elapsed since January 1, 1970.

Tips

  • It is difficult to calculate today's date from getTime(). See the full javascript Date() class instead.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org