Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add strftime stuff

  • Loading branch information...
commit 04c7477505d3998b9ab0ebdbdd321407f2af940a 1 parent 3c996a7
@Caged authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 datetime/format.js
View
31 datetime/format.js
@@ -0,0 +1,31 @@
+Object.extend(Date.prototype, {
+ /**
+ * @param format {String} The string used to format the date.
+ * Example: new Date().strftime("%A %I:%M %p")
+ */
+ strftime: function(format) {
+ var day = this.getDay(), month = this.getMonth();
+ var hours = this.getHours(), minutes = this.getMinutes();
+ function pad(num) { return num.toPaddedString(2); };
+
+ return format.gsub(/\%([aAbBcdHImMpSwyY])/, function(part) {
+ switch(part[1]) {
+ case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
+ case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
+ case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
+ case 'B': return $w("January February March April May June July August September October November December")[month]; break;
+ case 'c': return this.toString(); break;
+ case 'd': return pad(this.getDate()); break;
+ case 'H': return pad(hours); break;
+ case 'I': return pad((hours + 12) % 12); break;
+ case 'm': return pad(month + 1); break;
+ case 'M': return pad(minutes); break;
+ case 'p': return hours > 12 ? 'PM' : 'AM'; break;
+ case 'S': return pad(this.getSeconds()); break;
+ case 'w': return day; break;
+ case 'y': return pad(this.getFullYear() % 100); break;
+ case 'Y': return this.getFullYear().toString(); break;
+ }
+ }.bind(this));
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.