Return to Snippet

Revision: 45467
at May 1, 2011 05:47 by trusktr


Initial Code
var month_names = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
		now = new Date(),
		dayNum = now.getDate(),
		dayNum_str = dayNum.toString(),
		dayNum_strlen = dayNum_str.length,
		dayNum_lastDigit = dayNum_str.substring(dayNum_strlen-1),
		dayNum_isTeens = null,
		sup = '',
		thisMonth = now.getMonth(),
		thisYear = now.getFullYear();
	
	if (dayNum > 10 && dayNum < 14) {
		dayNum_isTeens = true;
	}
	if (!dayNum_isTeens) {
		if (dayNum_lastDigit == 1) {
		   sup = 'st';
		}
		else if (dayNum_lastDigit == 2) {
		   sup = 'nd';
		}
		else if (dayNum_lastDigit == 3) {
		   sup = 'rd';
		}
	}
	else {
	   sup = 'th';
	}
var date = ''+ month_names[thisMonth] + ' ' + dayNum + '<sup>' + sup + '</sup>, ' + thisYear;

document.write(date);

Initial URL

                                

Initial Description
Example: April 5th, 2011

Initial Title
Nice formatted dates in Javascript.

Initial Tags
javascript, date

Initial Language
JavaScript