Justin Palmer hat ein Snippet für Prototype gepostet welches das Date Object um strftime() erweitert.

JAVASCRIPT:
  1. Object.extend(Date.prototype, {
  2.   strftime: function(format) {
  3.     var day = this.getDay(), month = this.getMonth();
  4.     var hours = this.getHours(), minutes = this.getMinutes();
  5.     function pad(num) { return num.toPaddedString(2); };
  6.  
  7.     return format.gsub(/\%([aAbBcdHImMpSwyY])/, function(part) {
  8.       switch(part[1]) {
  9.         case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
  10.         case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
  11.         case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
  12.         case 'B': return $w("January February March April May June July August September October November December")[month]; break;
  13.         case 'c': return this.toString(); break;
  14.         case 'd': return pad(this.getDate()); break;
  15.         case 'H': return pad(hours); break;
  16.         case 'I': return pad((hours + 12) % 12); break;
  17.         case 'm': return pad(month + 1); break;
  18.         case 'M': return pad(minutes); break;
  19.         case 'p': return hours> 12 ? 'PM' : 'AM'; break;
  20.         case 'S': return pad(this.getSeconds()); break;
  21.         case 'w': return day; break;
  22.         case 'y': return pad(this.getFullYear() % 100); break;
  23.         case 'Y': return this.getFullYear().toString(); break;
  24.       }
  25.     }.bind(this));
  26.   }
  27. });

[via Justin Palmer]

Schön gelöst. Weitere ähnliche Schnippsel von ihm gibt es im GitHub.

Diesen Eintrag kommentieren