Format date with format String - Node.js Date

Node.js examples for Date:Date Format

Description

Format date with format String

Demo Code

function formatDate(formatDate, formatString, adjustGMT) {
    if (adjustGMT) {
        formatDate = new Date(formatDate);

        var sign = (formatDate.getTimezoneOffset() > 0) ? 1 : -1;
        var offset = Math.abs(formatDate.getTimezoneOffset());
        var hours = Math.floor(offset / 60);

        formatDate.setHours(formatDate.getHours() + (sign * hours));
    }//from w ww .  j  a v  a  2 s.co m
    if (formatDate instanceof Date) {
        var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
        var yyyy = formatDate.getFullYear();
        var yy = yyyy.toString().substring(2);
        var m = formatDate.getMonth() + 1;
        var mm = m < 10 ? "0" + m : m;
        var mmm = months[m - 1];
        var d = formatDate.getDate();
        var dd = d < 10 ? "0" + d : d;

        var h = formatDate.getHours();
        var hh = h < 10 ? "0" + h : h;
        var n = formatDate.getMinutes();
        var nn = n < 10 ? "0" + n : n;
        var s = formatDate.getSeconds();
        var ss = s < 10 ? "0" + s : s;

        if (formatString == null)
            formatString = joolaio.dateformat;

        formatString = formatString.replace(/yyyy/i, yyyy);
        formatString = formatString.replace(/yy/i, yy);
        formatString = formatString.replace(/mmm/i, mmm);
        formatString = formatString.replace(/mm/i, mm);
        //formatString = formatString.replace(/m/i, m);
        formatString = formatString.replace(/dd/i, dd);
        //formatString = formatString.replace(/d/i, d);
        formatString = formatString.replace(/hh/i, hh);
        //formatString = formatString.replace(/h/i, h);
        formatString = formatString.replace(/nn/i, nn);
        //formatString = formatString.replace(/n/i, n);
        formatString = formatString.replace(/ss/i, ss);
        //formatString = formatString.replace(/s/i, s);

        return formatString;
    } else {
        return "";
    }
}

/**
 * Used to format date according to standard formatting options
 *
 * @method formatDate
 * @param {string} formatDate date to format
 * @param {string} formatString format to convert the date by
 * @param {bool} adjustGMT should GMT adjustments be applied
 * @return {string} formatted date
 */
exports.formatDate = formatDate;

Date.prototype.fixDate = function (force, direction) {
    date = this;
    if (!force)
        return date;

    if (!direction)
        var sign = (date.getTimezoneOffset() > 0) ? 1 : -1;
    else
        var sign = (date.getTimezoneOffset() > 0) ? -1 : 1;
    var offset = Math.abs(date.getTimezoneOffset());
    var hours = Math.floor(offset / 60);

    date.setHours(date.getHours() + (sign * hours));

    return date;
};

function pad(str, char, length) {
    while (str.length < length)
        str = char + str;
    return str;
}

Related Tutorials