Frage

This is probably a simply answer but its late and I can't seem to work around it. I'm writing a code on a HTML5 canvas using javascript and I am supposed to add a canvas text script that should contain today's date and the current time.

This is what I have so far:

var c = document.getElementById("myCanvas");
var ctx=c.getContext("2d");
today = new Date();
var m = today.getMinutes();
var h = today.getHours(); 
var d = today.getDate()
var n = today.getMonth()+1
var y = today.getYear()
ctx.fillText(("Time: ", h,":", m), 325, 300);
ctx.fillText(("Date: ", d,"/", n,"/", y), 325, 350);

I am able to get the minute (m) displayed and the year (y) at the location I provided but I can't see the hour, date, month or even the ':' and 'Time/Date'. Help will be much appreciated.

War es hilfreich?

Lösung

You can't concatenate variables into strings with commas.

All the commas should be plusses to concatenate the strings

ctx.fillText(("Time: " + h +":" + m), 325, 300);
ctx.fillText(("Date: " + d + "/" + n + "/" + y), 325, 350);

FIDDLE

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top