Datum formatieren

Immer wieder tritt in JS das Problem auf, dass man ein Date-Objekt in einem gewissen Format ausgeben will. Genau dieses Problem will ich hier lösen:

Die Syntax

Am einfachsten ist es sich hier an gegebenen Schemata zu orientieren - ich nehme einfach mal die von strFTime aus PHP.

Code:ausführenändern Number.prototype.toZeros = function(vorKomma, nachKomma, fill){ if(!fill) fill = "0"; fill = fill.toString(); if (fill.length > 1) fill = fill.substring(0,1); var zahl = Math.abs(this); var minus = (this < 0); if (isNaN(nachKomma)) nachKomma = 0; zahl = zahl.toFixed(nachKomma); while (zahl.replace(/[\.,].+/, "").length < vorKomma){ zahl = fill + zahl.toString(); } if (minus) zahl = "-" + zahl; return zahl; }; Date.prototype.format = (function(){ var _date; var weekDays = {l: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], s:["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]}; var months = {l: ["Jannuary", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], s: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]} function abbr(m, f){ switch (f){ case "a": return weekDays.s[_date.getDay()]; case "A": return weekDays.l[_date.getDay()]; case "w": return _date.getDay(); case "u": return (_date.getDay() + 6) % 7 + 1; case "d": return _date.getDate().toZeros(2); case "e": return _date.getDate(); case "j": var helpDate = new Date(_date.getTime()); helpDate.setMonth(1); helpDate.setDate(1); return ((_date - helpDate) / (1000 * 3600 * 24)) + 1; case "U": case "V": case "W": return ""; case "b": case "h": return months.s[_date.getMonth()]; case "B": return months.l[_date.getMonth()]; case "m": return (_date.getMonth() + 1).toZeros(2); case "C": return Math.floor(_date.getFullYear() / 100); case "g": case "G": return ""; case "y": return _date.getFullYear().toString().substr(2); case "Y": return _date.getFullYear().toZeros(4); case "H": return _date.getHours().toZeros(2); case "I": return ((_date.getHours() + 11) % 12 + 1).toZeros(2); case "i": return (_date.getHours() + 11) % 12 + 1; case "M": return _date.getMinutes().toZeros(2); case "p": return (_date.getHours() < 12)? "AM": "PM"; case "P": return (_date.getHours() < 12)? "am": "pm"; case "r": return kkjs.date.format(_date, "%I:%M:%S %p"); case "R": return kkjs.date.format(_date, "%H:%M"); case "S": return _date.getSeconds().toZeros(2); case "T": return kkjs.date.format(_date, "%H:%M:%S"); case "X": case "c": case "x": return ""; case "z": case "Z": return (_date.getTimezoneOffset() / 60).toZeros(2); case "D": return kkjs.date.format(_date, "%m/%d/%y"); case "F": return kkjs.date.format(_date, "%Y-%m-%d"); case "s": return Math.floor(_date.getTime() / 1000); case "n": return "\n"; case "t": return "\t"; case "%": return "%"; default: return f; } } return function formatDate(format){ _date = this; return format.replace(/%(.)/g, abbr); }; })(); var d = new Date(); alert(d.format("%d.%m.%Y"));

Somit haben wir das (fast - es fehlen %U, %V, %W, %g, %G (theoretisch implementierbar - nur etwas aufwändiger), %X, %c und %x (wegen der Absenz eines "locale")) kompette Spektrum dieser schönen Datumsformatierfunktion in JS integriert.