Lodash: Why not add the Date format function?

Created on 16 Jan 2016  路  3Comments  路  Source: lodash/lodash

Just like: new Date().format('yyyy/MM/dd'), new Date().format('hh:mm:ss')

Here's code from total.js framewrok:

/**
 * Format datetime
 * @param {String} format
 * @return {String}
 */
Date.prototype.format = function(format) {

    var self = this;
    var half = false;

    if (format && format[0] === '!') {
        half = true;
        format = format.substring(1);
    }

    if (format === undefined || format === null || format === '')
        return self.getFullYear() + '-' + (self.getMonth() + 1).toString().padLeft(2, '0') + '-' + self.getDate().toString().padLeft(2, '0') + 'T' + self.getHours().toString().padLeft(2, '0') + ':' + self.getMinutes().toString().padLeft(2, '0') + ':' + self.getSeconds().toString().padLeft(2, '0') + '.' + self.getMilliseconds().toString().padLeft(3, '0') + 'Z';

    var h = self.getHours();

    if (half) {
        if (h >= 12)
            h -= 12;
    }

    return format.replace(regexpDATEFORMAT, function(key) {
        switch (key) {
            case 'yyyy':
                return self.getFullYear();
            case 'yy':
                return self.getYear();
            case 'MM':
                return (self.getMonth() + 1).toString().padLeft(2, '0');
            case 'M':
                return (self.getMonth() + 1);
            case 'dd':
                return self.getDate().toString().padLeft(2, '0');
            case 'd':
                return self.getDate();
            case 'HH':
            case 'hh':
                return h.toString().padLeft(2, '0');
            case 'H':
            case 'h':
                return self.getHours();
            case 'mm':
                return self.getMinutes().toString().padLeft(2, '0');
            case 'm':
                return self.getMinutes();
            case 'ss':
                return self.getSeconds().toString().padLeft(2, '0');
            case 's':
                return self.getSeconds();
            case 'w':
            case 'ww':
                var tmp = new Date(+self);
                tmp.setHours(0, 0, 0);
                tmp.setDate(tmp.getDate() + 4 - (tmp.getDay() || 7));
                tmp = Math.ceil((((tmp - new Date(tmp.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
                if (key === 'ww')
                    return tmp.toString().padLeft(2, '0');
                return tmp;
            case 'a':
                var a = 'AM';
                if (self.getHours() >= 12)
                    a = 'PM';
                return a;
        }
    });
};
question

Most helpful comment

I rather point folks to moment.js.

All 3 comments

I rather point folks to moment.js.

Thanks!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Alexsey picture Alexsey  路  3Comments

jcubic picture jcubic  路  3Comments

andreineculau picture andreineculau  路  3Comments

bryandowning picture bryandowning  路  3Comments

gajus picture gajus  路  3Comments