javascript - Date and Time Script -


i'm trying produce date , time script js beginners, i'm sure of have seen before.

i'm not sure i've gone wrong , struggling problem solve log spits out 'error'.

can clear me, sticking general way of writing script. explanations great!

var date = new date();  var day = date.getday(); var hour = date.gethours(); var min = date.getminutes(); var sec = date.getseconds();  var weekdays = [sunday, monday, tuesday, wednesday, thursday, friday, saturday]; var today = weekdays[day];  var ampm = ampmfunc; function ampmfunc() {     if(hour < 12) {         ampm = am;     } else {          ampm = pm;     } }  console.log("today is: " + today); console.log("current time is: " + hour + ampm + ":" + min + ":" + "sec"); 

first of all, forgot enclose strings ''. i.e. sunday should 'sunday'.

also - remember - javascript is case sensetive. variable called ampm not recognized ampm.

also, function ampmfunc should return value, rather set global variable's value. it's neater way. , when call it, call brackets: var ampm = ampmfunc();.

but other - job! ☻

see fixed example:

var date = new date();    var day = date.getday();  var hour = date.gethours();  var min = date.getminutes();  var sec = date.getseconds();    var weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];  var today = weekdays[day];    var ampm = ampmfunc();    function ampmfunc() {      if(hour < 12)           return 'am';      else           return 'pm';  }    console.log("today is: " + today);  console.log("current time is: " + hour + ampm + ":" + min + ":" + "sec");


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -