Helper Functions

Contact Us or call 1-877-932-8228
Helper Functions

Helper Functions

Some languages have functions that return the month as a string. JavaScript doesn't have such a built-in function. The sample below shows a user-defined "helper" function that handles this and how the getMonth() method of a Date object can be used to get the month.

Code Sample:

BuiltInObjects/Demos/MonthAsString.html
---- C O D E   O M I T T E D ----

<script type="text/javascript">
	function monthAsString(num){
		var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
		return months[num-1];
	}

	function enterMonth(){
		var userMonth = prompt("What month were you born?", "");
		alert("You were born in " + monthAsString(userMonth) + ".");
	}
	
	function getCurrentMonth(){
		var today = new Date();
		alert(monthAsString(today.getMonth()+1));
	}
</script>
---- C O D E   O M I T T E D ----
Next