Learn Scripting Languages from the Master!
This page shows how to use number of time-related methods available in JavaScript. We use the methods to get the current year, month, date, day of the week, hour, minutes, and seconds.
The following shows the output of the script code shown below:
Step 1: place the following code in the head section (between <head> and </head>) of your web document.
<script language="javascript"> function DateAndTimeFunctions () { var dateObj = new Date(); var strOut; strOut = "Year: " + dateObj.getFullYear(); strOut = strOut + "<br>Month: " + CurrentMonth (); strOut = strOut + "<br>Day of the month: " + dateObj.getDate(); strOut = strOut + "<br>Day of the week: " + GetDayName(); strOut = strOut + "<br>Hour of the day (0 - 23): " + dateObj.getHours(); strOut = strOut + "<br>Minutes (0 - 59): " + dateObj.getMinutes(); strOut = strOut + "<br>Seconds (0 - 59): " + dateObj.getSeconds(); return strOut; } function CurrentMonth () { var dateObj = new Date(); var monthsArr = new Array(); var currMonth = dateObj.getMonth(); monthsArr[0] = "January"; monthsArr[1] = "February"; monthsArr[2] = "March"; monthsArr[3] = "April"; monthsArr[4] = "May"; monthsArr[5] = "June"; monthsArr[6] = "July"; monthsArr[7] = "August"; monthsArr[8] = "September"; monthsArr[9] = "October"; monthsArr[10] = "November"; monthsArr[11] = "December"; return monthsArr[currMonth]; } function GetDayName () { var dateObj = new Date(); var weekDaysArr = new Array(); var currDay = dateObj.getDay(); weekDaysArr[0] = "Sunday"; weekDaysArr[1] = "Monday"; weekDaysArr[2] = "Tuesday"; weekDaysArr[3] = "Wednesday"; weekDaysArr[4] = "Thursday"; weekDaysArr[5] = "Friday"; weekDaysArr[6] = "Saturday"; return weekDaysArr[currDay]; } </script>Step 2: Call the GetDayName () function from your web page:
<script language="javascript" type="text/javascript">document.write (DateAndTimeFunctions ());</script>