Unix-Timestamp in JavaScript formatieren

Um ein Unix-Timestamp mit JavaScript zu formatieren, habe ich mir folgendes Objekt geschrieben:

var timeFormat = {
    /**
     * Output format.
     *
     * d - current day
     * m - current month
     * Y - current Year
     *
     * H - current hour (24 hour system)
     * i - current minute
     * s - current second
     */
    displayFormat: 'd.m.Y - H:i:s',

    /**
     * Format given unix-timestamp
     */
    format: function(timestamp) {
        // convert unix timestamp to javascript date object
        var d = new Date(timestamp * 1000);
        var output = this.displayFormat;
        
        output = output.replace(/d/g, this.padZero(d.getDate()))
            .replace(/m/g, this.padZero(d.getMonth()+1))
            .replace(/Y/g, d.getFullYear())
            .replace(/H/g, this.padZero(d.getHours()))
            .replace(/i/g, this.padZero(d.getMinutes()))
            .replace(/s/g, this.padZero(d.getSeconds()));

        
        return output;

    },
   
    /**
     * add zero paddings to numbers smaller than 10
     */
    padZero: function(number) {
        if (number < 10) {
            return "0" + number.toString();
        }
        
        return number;
    }
};

Die Benutzung ist ziemlich einfach:

// schreibt 13.09.2011 - 13:30:23
document.write(timeFormat.format(1318473023));

Man kann das Ausgabeformat über das timeFormat.displayFormat Feld steuern:

timeFormat.displayFormat = 'H:i'; // gibt nur Stunden und Minuten aus
document.write(timeFormat.format(1318473023)); // schreibt 13:30

Wie bereits oben beschrieben sind folgende Zeichen möglich (auch mehrfach)

    /**
     * Ausgabeformat
     *
     * d - aktueller Tag
     * m - aktueller Monat
     * Y - aktuelles Jahr
     *
     * H - aktuelle Stunde
     * i - aktuelle Minute
     * s - aktuelle Sekunde
     */