I have to do an exercise on dates and times in Javascript. This is the text of the exercise:
Write instructions that allow you to view (with the document.write () method) the current date and time in this way: Saturday 30 April 2011, 12:05
I declared two variables, months and days. Then I created two arrays, one containing the months and the other containing the days of the week. Then I tried to extract the day of the week and the month from the arrays.
var date = new Date();
var set, gg, mm, aaaa, h, m;
var month = new Array(12);
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var days = new Array(7);
days = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"];
set = days[date.getDay()] + " ";
gg = date.setDate(6) + " ";
mm = month[date.setMonth(3)] + " ";
aaaa = date.setYear(2011);
h = date.setHours(12) + ":";
m = date.setMinutes(05);
document.write(set + gg + mm + aaaa + ", " + h + m);
I expect the output of this code to be "Saturday 30 April 2011, 12:05"
, but the actual output is "Sat 1565123795470 undefined 1302122195470, 1302086195470:1302084335470"
.