iEntry 10th Anniversary CSS Snippets JavaScript Snippets

 
 





Working with Dates

JavaScript has a built in function that allows you to work with dates. This feature is great for creating a calender, to display the current date, or to create a clock. You can also use the date function to set the date in your script.

Date Objects
Method Description
getDate()Returns the current date and time
getFullYear()Returns the four digit year
getYear()Returns the year in two digit format, ie 08
getUTCFullYearReturns the four digit year according to universal time.
getUTCYearReturns the year in two digit format according to universal time
getMonthReturns the month as a number, between 0 and 11
getUTCMonthReturns the month as a number according to universal time
getDateReturns the day of the month.
getUTCDateReturns the day of the month according to universal time
getDay()Returns the day of the week as a number between 0-6
getUTCDay()Returns the day of the week according to universal time

Time Objects
Method Description
getHour()Returns the hour in 24-hour format
getUTCHour()Returns the hour according to universal time in 24 hour format
getMinute()Returns the minute between 0 and 59
getUTCMinute()Returns the minute according to universal time
getSecond()Returns the second between 0 and 59
getUTCSecond()Returns the second according to universal time
getMillisecond()Returns the millisecond between 0 and 999
getUTCMillisecond()Returns the millisecond according to universal time
getTimezoneOffset()Returns the difference in minutes between GMT and local time

If you want to set a date, you would just change the get to set in any of the methods above, and input the value you want to set between the parentheses. For example, if I needed to set the current date to July 4, 1776, I would use setDate("July 4, 1776"). Now, lets use some of the date methods to display the current date.

<script language="JavaScript" type="text/javascript">

	var days = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
	var months = new Array("January","February","March","April","May","June","July","August","October","November","December")

	var now = new Date();
	var currentMonth = now.getMonth()
	var currentYear = now.getFullYear()
	var currentDay = now.getDate()
	var currentWeekDay = now.getDay();
	
document.write('Today is ' +days[currentWeekDay] + '. '+ months[currentMonth] + ' ' + currentDay + ', ' + currentYear)
</script>

The first thing we do is set up two arrays. One for the days of the week, the other is for the months of the year. We do this because we want to display the day of the week and the month as text, and not as a number. When we get the day of the week or month we can use that variable to call the correct text for each because of the array.
Next, we create the variable now, and set it to the current date, The next four variables we are getting the current month, year, day of the month, and day of the week. Finally, we write this information to the browser. Below is what the output would look like.