#

Javascript Dateformatting

I just recognized that there is no convenient method in Javascript to format a date-object to a string with the help of a dateformat-pattern, like in java.text.SimpleDateFormat. So I have writen a simple method, which just does this, including most important patterns.

/*
Usage:
formatDate(new Date(), 'yyyy/MM/dd HH:mm:ss.SSS');
will return current date as something like '2010/01/01 14:41:52.957'.
 
Implemented:
y  	Year 	Year  	1996 (by using yyyy); 96 (by using yy)
M 	Month in year 	Month 	7 or 10 (by using M); 07 or 10 (by using MM)
d 	Day in month 	Number 	7 or 10 (by using d); 07 or 10 (by using dd)
H 	Hour in day (0-23) 	Number 	7 or 10 (by using H); 07 or 10 (by using HH)
k 	Hour in day (1-24) 	Number 	7 or 10 (by using k); 07 or 10 (by using kk)
m 	Minute in hour 	Number 	7 or 10 (by using m); 07 or 10 (by using mm)
s 	Second in minute 	Number 	7 or 10 (by using s); 07 or 10 (by using ss)
S 	Millisecond 	Number 	 7 or 10 or 100 (by using S); 07 or 10 or 100 (by using SS); 007 or 010 or 100 (by using SSS);
 
Not implemented:
M 	Month in year 	Month 	July; Jul; textual values (by using MMM or MMMM)
G  	Era designator 	Text  	AD
w 	Week in year 	Number 	27
W 	Week in month 	Number 	2
D 	Day in year 	Number 	189
F 	Day of week in month 	Number 	2
E 	Day in week 	Text 	Tuesday; Tue
a 	Am/pm marker 	Text 	PM
K 	Hour in am/pm (0-11) 	Number 	0
h 	Hour in am/pm (1-12) 	Number 	12
 * */
function formatDate(date, format){
	var dayInMonth = date.getDate();
	var fullDayInMonth = dayInMonth+'';
	if (fullDayInMonth.length<2){
		fullDayInMonth = "0"+fullDayInMonth;
	}
	var month = date.getMonth()+1;
	var fullMonth = month+'';
	if (fullMonth.length<2){
		fullMonth = "0"+fullMonth;
	}
	var fullYear = date.getFullYear()+'';
	var year = fullYear.substring(2);
	var hour = date.getHours();
	var fullHour = hour+'';
	if (fullHour.length<2){
		fullHour = "0"+fullHour;
	}
	var khour = hour+1;
	var fullKhour = khour+'';
	if (fullKhour.length<2){
		fullKhour = fullKhour+'';
	}
 
	var minutes = date.getMinutes();
	var fullMinutes = minutes+'';
	if (fullMinutes.length<2){
		fullMinutes = "0"+fullMinutes;
	}
	var seconds = date.getSeconds();
	var fullSeconds = seconds+'';
	if (fullSeconds.length<2){
		fullSeconds = "0"+seconds;
	}
	var millis = date.getMilliseconds();
	var fullMillis1 = millis+'';
	if (fullMillis1.length<2){
		fullMillis1 = '0'+fullMillis1;
	}
	var fullMillis2 = fullMillis1;
	if (fullMillis2.length<3){
		fullMillis2 = '0'+fullMillis2;
	}
 
	var result = format;
	result = result.replace(/MM/g, fullMonth);
	result = result.replace(/M/g, month);
	result = result.replace(/yyyy/g, fullYear);
	result = result.replace(/yy/g, year);
	result = result.replace(/dd/g, fullDayInMonth);
	result = result.replace(/d/g, dayInMonth);
	result = result.replace(/HH/g, fullHour);
	result = result.replace(/H/g, hour);
	result = result.replace(/kk/g, fullKhour);
	result = result.replace(/k/g, khour);
	result = result.replace(/mm/g, fullMinutes);
	result = result.replace(/m/g, minutes);
	result = result.replace(/ss/g, fullSeconds);
	result = result.replace(/s/g, seconds);
	result = result.replace(/SSS/g, fullMillis2);
	result = result.replace(/SS/g, fullMillis1);
	result = result.replace(/S/g, millis);
 
	return result;
}

Leave a Reply »»

Note: All comments are manually approved to avoid spam. So if your comment doesn't appear immediately, that's ok. Have patience, it can take some days until I have the time to approve my comments.