Skip to content

Instantly share code, notes, and snippets.

@jsmecham
Created February 24, 2010 18:09
Show Gist options
  • Save jsmecham/313675 to your computer and use it in GitHub Desktop.
Save jsmecham/313675 to your computer and use it in GitHub Desktop.
Date#strftime for Prototype
Date.monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'
];
Date.dayNames = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday'
];
Object.extend(Date.prototype,
{
strftime: function(format)
{
var syntax = /(^|.|\r|\n)(%([A-Za-z]{1,2}))/,
components = {
a: Date.dayNames[this.getDay()].substring(0, 3),
A: Date.dayNames[this.getDay()],
b: Date.monthNames[this.getMonth()].substring(0, 3),
B: Date.monthNames[this.getMonth()],
d: this.getDate(),
dd: this.getDate().toPaddedString(2),
H: this.getHours() % 12 || 12,
HH: (this.getHours() % 12 || 12).toPaddedString(2),
I: this.getHours().toPaddedString(2),
m: this.getMonth(),
mm: this.getMonth().toPaddedString(2),
M: this.getMinutes(),
MM: this.getMinutes().toPaddedString(2),
p: this.getHours() >= 12 ? 'PM' : 'AM',
S: this.getSeconds(),
SS: this.getSeconds().toPaddedString(2),
w: this.getDay(),
y: this.getFullYear().toString().substring(2, 4),
Y: this.getFullYear()
};
return format.interpolate(components, syntax);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment