/**
 * Return a formatted date string 
 *
 * Arguments:
 *   - date: A JavaScript Date object; if you have a unix timestamp, you want "new Date(timestamp * 1000)"
 *   - format: The format string; this is a subset of PHP's date formatting strings,
 *        see http://www.php.net/manual/en/function.date.php for a reference
 *
 * Author and License:
 *   - author: Luke Sneeringer, FeedMagnet (luke@feedmagnet.com)
 *   - license: New BSD License (http://www.opensource.org/licenses/bsd-license.php)
 */
function formatDate(date, format) {
    // return a number as a string leading zeros when necessary
    function _pad(n, digits) {
        n = n.toString()
        while (n.length < digits) {
            n = '0' + n
        }
        return n
    }

    // the working response
    var response = format

    // escape the characters I supported
    var supported = 'djDNwlFmMnyYgGhHisaA'
    for (var i = 0; i < supported.length; i += 1) {
        var ch = supported.charAt(i)
        response = response.replace(ch, '%' + ch)
    }

    // day of the month
    if (format.indexOf('d') > -1 || format.indexOf('j') > -1) {
        response = response.replace('%j', date.getDate().toString())
        response = response.replace('%d', _pad(date.getDate(), 2))
    }

    // day of the week
    if (format.indexOf('D') > -1 || format.indexOf('N') > -1 || format.indexOf('w') > -1 || format.indexOf('l') > -1) {
        var d_short = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
        var d_long = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

        response = response.replace('%w', date.getDay().toString())
        response = response.replace('%N', (date.getDay() + 1).toString())
        response = response.replace('%D', d_short[date.getDay()])
        response = response.replace('%l', d_long[date.getDay()])
    }

    // month
    if (format.indexOf('F') > -1 || format.indexOf('m') > -1 || format.indexOf('M') > -1 || format.indexOf('n') > -1) {
        var m_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        var m_long = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

        response = response.replace('%F', m_long[date.getMonth()])
        response = response.replace('%m', _pad(date.getMonth() + 1, 2))
        response = response.replace('%M', m_short[date.getMonth()])
        response = response.replace('%n', (date.getMonth() + 1).toString())
    }

    // year
    if (format.indexOf('y') > -1 || format.indexOf('Y') > -1) {
        response = response.replace('%y', date.getFullYear().toString().substr(2))
        response = response.replace('%Y', date.getFullYear())
    }

    // hours
    if (format.indexOf('g') > -1 || format.indexOf('G') > -1 || format.indexOf('h') > -1 || format.indexOf('H') > -1) {
        var hours = date.getHours()
        response = response.replace('%G', hours.toString())
        response = response.replace('%H', _pad(hours, 2))

        // get 12-hour time
        if (hours == 0) { hours = 12 }
        if (hours > 12) { hours -= 12 }
        response = response.replace('%g', hours.toString())
        response = response.replace('%h', _pad(hours, 2))
    }

    // minutes
    if (format.indexOf('i') > -1) {
        response = response.replace('%i', _pad(date.getMinutes(), 2))
    }

    // seconds
    if (format.indexOf('s') > -1) {
        response = response.replace('%s', _pad(date.getSeconds(), 2))
    }

    // AM and PM
    if (format.indexOf('a') > -1 || format.indexOf('A') > -1) {
        var m = date.getHours() > 0 && date.getHours < 12 ? 'am' : 'pm'
        response = response.replace('%a', m)
        response = response.replace('%A', m.toUpperCase())
    }

    return response
}
