Shuss Media Header
Date and Time Javascript date and time object
The date object is useful when we want to display or use a timestamp in some sort of calculation of the current date and time in relation to where we are at (local).

var curtime = new Date() outputs local date and time, i.e.,

Methods of date object that will help us get information
  1. getTime() - Returns the number of milliseconds since midnight Jan 1, 1970, i.e, 
  2. getHours() - Returns hours, i.e., 
  3. getMinutes() - Returns minutes, i.e.,
  4. getSeconds() - Returns seconds, i.e.,
  5. getDay() - Returns day of the week, i.e.,
  6. getDate() - Returns the date of the month, i.e.,
  7. getMonth() - Returns Number of month: 0-11, i.e.,
  8. getFullYear() - Returns the year, i.e.,
Time
  1. var time = curtime.toTimeString() //outputs time, i.e.,
  2. var time = curtime.toString() //outputs time, i.e.,
  3. var hours = curtime.getHours() //
  4. var mins = curtime.getMinutes() //
  5. var secs = curtime.getSeconds() //
Day, date, month, and year
  1. var day = curtime.getDay()
  2. var date = curtime.getDate()
  3. var mon = curtime.getMonth() + 1
  4. var year = curtime.getFullYear()
  5. document.write(day + " " + mon + "/" + date + "/" + year)

  • DISPLAY

  • EXPLANATION
  • Our day is in the form of number such as 1 is Mon, 2 is Tue and so on; 3 means Wednesday. But we want it to be in the day format.
So, I add,
  1. var weekday = new Array(7);
  2. weekday[0]="Sun";
  3. weekday[1]="Mon";
  4. weekday[2]="Tue";
  5. weekday[3]="Wed";
  6. weekday[4]="Thur";
  7. weekday[5]="Fri";
  8. weekday[6]="Sat";
  9. document.write(weekday[day]);
  • DISPLAY
  • NOW, we can print out
  • document.write(weekday[day] + "/" + mon + "/"+ date + "/" + year);
  • DISPLAY
  • EXPLANATION
  • Notice that the month displays in form of numerical order but if we want to display the name of the month such as January, February, and etc, we need to add more code as below.
So, I add,
  1. var month = new Array(12);
  2. month[0]="Jan";
  3. month[1]="Feb";
  4. month[2]="March";
  5. month[3]="April";
  6. month[4]="May";
  7. month[5]="June";
  8. month[6]="July";
  9. month[7]="Aug";
  10. month[8]="Sept";
  11. month[9]="Oct";
  12. month[10]="Nov";
  13. month[11]="Dec";
  14. document.write(month[mon-1]); B/c we add 1 to getMonth(). In the array we have to substract mon with 1.
  • DISPLAY Month = , just how we want it.
  • OR in the full form