Date Format //JavaScript Repository

Description

Formats a date according to a formatting string.
Created: 2006.05.05

Code (Download)

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/geral/date-format [rev. #1]

Date.prototype.format = function(m, r){
    var d = this, a, fix = function(n, c){return (n = n + "").length < c ? new Array(++c - n.length).join("0") + n : n};
    var r = r || {}, f = {j: function(){return d.getDate()}, w: function(){return d.getDay()},
        y: function(){return (d.getFullYear() + "").slice(2)}, Y: function(){return d.getFullYear()},
        n: function(){return d.getMonth() + 1}, m: function(){return fix(f.n(), 2)},
        g: function(){return d.getHours() % 12 || 12}, G: function(){return d.getHours()},
        H: function(){return fix(d.getHours(), 2)}, h: function(){return fix(f.g(), 2)},
        d: function(){return fix(f.j(), 2)}, N: function(){return f.w() + 1},
        i: function(){return fix(d.getMinutes(), 2)}, s: function(){return fix(d.getSeconds(), 2)},
        ms: function(){return fix(d.getMilliseconds(), 3)}, a: function(){return d.getHours() > 11 ? "pm" : "am"},
        A: function(){return f.a().toUpperCase()}, O: function(){return d.getTimezoneOffset() / 60},
        z: function(){return (d - new Date(d.getFullYear() + "/1/1")) / 864e5 >> 0},
        L: function(){var y = f.Y(); return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},
        t: function(){var n; return (n = d.getMonth() + 1) == 2 ? 28 + f.L() : n & 1 && n < 8 || !(n & 1) && n > 7 ? 31 : 30},
        W: function(){
            var a = f.z(), b = 364 + f.L() - a, nd = (new Date(d.getFullYear() + "/1/1").getDay() || 7) - 1;
            return (b <= 2 && ((d.getDay() || 7) - 1) <= 2 - b) ? 1 :
                (a <= 2 && nd >= 4 && a >= (6 - nd)) ? new Date(d.getFullYear() - 1 + "/12/31").format("%W%") :
                (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
        }
    }
    return m.replace(/%(.*?)%/g, function(t, s){
        return r[s] ? r[s](d, function(s){return f[s] && f[s]();}) : f[s] ? f[s]() : "%" + (s && s + "%");
    });
}

Example (Example)

<script type="text/javascript">
//<![CDATA[

var month = ["Janeiro", "Fevereiro", "Mar?o", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
var days = ["Domingo", "Segunda-Feira", "Ter?a-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira", "S?bado"];
var functions = {
    DayName: function(d, f){
        return days[f('w')];
    },
    MonthName: function(d, f){
        return month[f('n') - 1];
    },
    userFunction: function(d, f){
        return "User function called [" + d + "]";
    }
}

var date = new Date();
var m = "Hoje ? %DayName%, dia %j% de %MonthName% de %Y%.\n%H%:%i%:%s% : %ms%ms\n%userFunction%\n\n";
document.write(
    m = m.replace(/\n/g, "<br />"), "<hr />", 
    date.format(m, functions)
);

//]]>
</script>

Help

Date.prototype.format(format: String, [userFunctions: Object = null]): String
Formats a date according to a formatting string.
format
formatting string, accepts special characters, which must be enclosed by "%" (to use "%" literally, use "%%"), the following characters are recognized:
  • d - day of the month 2 digits (01-31)
  • j - day of the month (1-31)
  • N - 1=Monday, 2=Tuesday, etc (1-7)
  • w - 0=Sunday, 1=Monday (0-6)
  • z - day of the year (1-365)
  • W - week of the year (1-52)
  • m - 2 digit month number (01-12)
  • n - month number (1-12)
  • t - Days in the month (28-31)
  • L - leap year (0 no, 1 yes)
  • Y - four digit year (Ex. 1979, 2006)
  • y - two digit year (Ex. 79, 06)
  • a - am or pm
  • A - AM or PM
  • g - 12 hour (1-12)
  • G - 24 hour c (0-23)
  • h - 2 digit 12 hour (01-12)
  • H - 2 digit 24 hour (00-23)
  • i - 2 digit minutes (00-59)
  • s - 2 digit seconds (00-59)
  • ms - 3 digits milliseconds(000-999)
  • O - offset Timezone (hours)
userFunctions
object where the properties must be names of user functions, each property value must be a function which receives two arguments, the first will be the own Date object and the second if a function that receives as argument one of the special characters defined above and returns its value.

Rank (Votes: 23)

3.43