Created
February 24, 2010 18:09
-
-
Save jsmecham/313675 to your computer and use it in GitHub Desktop.
Date#strftime for Prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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