/ Published in: JavaScript
                    
                                        
Format a date into a string using several string variables.
Usage:
(new Date("6/6/2011")).format("%W, %B %d%o, %Y");
Result:
Monday, June 6th, 2011
                Usage:
(new Date("6/6/2011")).format("%W, %B %d%o, %Y");
Result:
Monday, June 6th, 2011
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
/* Date formatting v2.0
*
* This work is licensed under a Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/
*
* Author: Andy Harrison, http://dragonzreef.com/
* Date: 16 September 2011
*/
Date.prototype.getMonthName = function(){ return (["January","February","March","April","May","June","July","August","September","October","November","December"])[this.getMonth()]; }
Date.prototype.getUTCMonthName = function(){ return (["January","February","March","April","May","June","July","August","September","October","November","December"])[this.getUTCMonth()]; }
Date.prototype.getDayName = function(){ return (["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"])[this.getDay()]; }
Date.prototype.getUTCDayName = function(){ return (["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"])[this.getUTCDay()]; }
//if useUTC is true, UTC values will be used instead of local time values
Date.prototype.format = function(formatStr, useUTC)
{
/*
Format string variables:
%Y 4-digit year (e.g., 2011)
%y 2-digit year (e.g., 11)
%M 2-digit month (01-12)
%m month (1-12)
%B full month name (January-December)
%b abbreviated month name (Jan-Dec)
%D 2-digit day of month (01-31)
%d day of month (1-31)
%o ordinal of the day of month (st, nd, rd, th)
%W full weekday name (Sunday-Saturday)
%w abbreviated weekday name (Sun-Sat)
%I hour in 24-hour format (00-23)
%H 2-digit hour in 12-hour format (01-12)
%h hour in 12-hour format (1-12)
%P AM/PM
%p am/pm
%q a/p
%N 2-digit minute (00-59)
%n minute (0-59)
%S 2-digit second (00-59)
%s second (0-59)
%Z 3-digit milliseconds (000-999)
%z milliseconds (0-999)
%e UTC offset +/-
%F 2-digit hour offset (00-23)
%f hour offset (0-23)
%G 2-digit minute offset (00-59)
%g minute offset (0-59)
%% percent sign
*/
function pad(numStr, digits)
{
numStr = numStr.toString();
while(numStr.length < digits) numStr = "0"+numStr;
return numStr;
}
var theYear = useUTC ? this.getUTCFullYear() : this.getFullYear();
var theMonth = useUTC ? this.getUTCMonth() : this.getMonth();
var theMonthName = useUTC ? this.getUTCMonthName() : this.getMonthName();
var theDate = useUTC ? this.getUTCDate() : this.getDate();
var theDayName = useUTC ? this.getUTCDayName() : this.getDayName();
var theHour = useUTC ? this.getUTCHours() : this.getHours();
var theMinute = useUTC ? this.getUTCMinutes() : this.getMinutes();
var theSecond = useUTC ? this.getUTCSeconds() : this.getSeconds();
var theMS = useUTC ? this.getUTCMilliseconds() : this.getMilliseconds();
var theOffset = useUTC ? 0 : -this.getTimezoneOffset(); //offset in minutes
var v = /%(.)/, m, formatted = "", d, h;
while((m = v.exec(formatStr)) != null)
{
formatted += formatStr.slice(0, m.index);
switch(m[1])
{
case "Y": formatted += theYear; break;
case "y": formatted += theYear.toString().slice(-2); break;
case "M": formatted += pad(theMonth+1, 2); break;
case "m": formatted += theMonth+1; break;
case "B": formatted += theMonthName; break;
case "b": formatted += theMonthName.slice(0,3); break;
case "D": formatted += pad(theDate, 2); break;
case "d": formatted += theDate; break;
case "o":
d = theDate;
formatted += (d==1 || d==21 || d==31) ? "st" : (d==2 || d==22) ? "nd" : (d==3 || d==23) ? "rd" : "th";
break;
case "W": formatted += theDayName; break;
case "w": formatted += theDayName.slice(0,3); break;
case "I": formatted += pad(theHour, 2); break;
case "H":
h = theHour % 12;
if(h==0) h = 12;
formatted += pad(h, 2);
break;
case "h":
h = theHour % 12;
if(h==0) h = 12;
formatted += h;
break;
case "P": formatted += (theHour<12 ? "AM" : "PM"); break;
case "p": formatted += (theHour<12 ? "am" : "pm"); break;
case "q": formatted += (theHour<12 ? "a" : "p"); break;
case "N": formatted += pad(theMinute, 2); break;
case "n": formatted += theMinute; break;
case "S": formatted += pad(theSecond, 2); break;
case "s": formatted += theSecond; break;
case "Z": formatted += pad(theMS, 3); break;
case "z": formatted += theMS; break;
case "e": formatted += theOffset < 0 ? "-" : "+"; break; //if offset==0, it will be "+"
case "F": formatted += pad(Math.floor(Math.abs(theOffset)/60), 2); break;
case "f": formatted += Math.floor(Math.abs(theOffset)/60); break;
case "G": formatted += pad(theOffset%60, 2); break;
case "g": formatted += theOffset%60; break;
case "%": formatted += "%"; break;
default: formatted += m[0];
}
formatStr = formatStr.slice(m.index+2);
}
formatted += formatStr;
return formatted;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                