Aven Dyke Aven Dyke - 5 months ago 15
Javascript Question

Trying to make a clock in JavaScript, getting an error, Uncaught TypeError: undefined is not a function

Clock script is very basic, I have:

function time() {
var time = new Date().getTime();
var ms = time.substring(11, 13)
var secs = time.substring(0, 10)
}


When using
substring
, it returns the error "undefined is not a function"
I need a way to get that "time" value to be set to the actual returned number, not the
new Date().GetTime();
returning it.

Answer

You can't use substring() because the time isn't a string. Convert it to a string first:

var time = ''+(new Date().getTime());
Comments