Nodejs String Format format( formatString)

Here you can find the source of format( formatString)

Method Source Code

Date.prototype.format = function ( formatString) {
   var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
   var yyyy = this.getFullYear();
   var yy = yyyy.toString().substring(2);
   var m = this.getMonth();
   var mm = m < 10 ? "0" + m : m;
   var mmm = months[m];
   var d = this.getDate();
   var dd = d < 10 ? "0" + d : d;
   //from  w w  w .j a v a 2s  .  c  o m
   var h = this.getHours();
   var hh = h < 10 ? "0" + h : h;
   var n = this.getMinutes();
   var nn = n < 10 ? "0" + n : n;
   var s = this.getSeconds();
   var ss = s < 10 ? "0" + s : s;

   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;
};

Related

  1. format()
    String.format = function() {
      var s = arguments[0];
      for (var i = 0; i < arguments.length - 1; i++) {       
        var reg = new RegExp("\\{" + i + "\\}", "gm");             
        s = s.replace(reg, arguments[i + 1]);
      return s;
    
  2. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
    };
    
  3. format()
    String.prototype.format=function(){
      var a=arguments;
      return this.replace(/\{(\d+)\}/g,function(b,c){return a[c]})
    };
    
  4. format()
    String.prototype.format = function() {
      a = this;
      for (k in arguments) {
        a = a.replace("{" + k + "}", arguments[k]);
      return a;
    };