Nodejs String Format format(string)

Here you can find the source of format(string)

Method Source Code

function ms_to_days(m) {
  return Math.floor(m / (86400 * 1000) + 0.5)
}

function date_period_to_ms(dp) {
  var ym = dp.split('-', 2)
  return new Date(ym[0], parseInt(ym[1], 10) - 1, 1);
}

Date.prototype.format = function (string) {  
  string = string.replace(/%b/, I18n.t('date.abbr_month_names')[this.getMonth() + 1], 'g');
  string = string.replace(/%B/, I18n.t('date.month_names')[this.getMonth() + 1]     , 'g');
  string = string.replace(/%d/, ('0' + this.getDate()).substr(-2)                   , 'g');
  string = string.replace(/%m/, ('0' + (this.getMonth() + 1)).substr(-2)            , 'g');
  string = string.replace(/%Y/, this.getFullYear()                                  , 'g');
  string = string.replace(/%H/, ('0' + this.getHours()).substr(-2)                  , 'g');
  string = string.replace(/%M/, ('0' + this.getMinutes()).substr(-2)                , 'g');
  string = string.replace(/%S/, ('0' + this.getSeconds()).substr(-2)                , 'g');
  string = string.replace(/%I/, ('0' + (this.getHours() > 12 ?
                                          this.getHours() - 12 :
                                          this.getHours() == 0 ?
                                            12 :
                                            this.getHours())).substr(-2)            , 'g');
  string = string.replace(/%p/, I18n.t('time.'+(this.getHours() < 12) ? 'am' : 'pm'), 'g');
  return string;// www  .j a va 2s . co  m
}

Date.from_date_period = function(dp) {
  return new Date(date_period_to_ms(dp));
}

Date.prototype.to_date_period = function() {
  return this.getFullYear() + '-' + ('0' + (this.getMonth() + 1).toString().substr(-2));
}

Date.today = function() {
  var now = new Date();
  return new Date(now.getFullYear(), now.getMonth(), now.getDate());
}

Related

  1. format(parameters)
    var FORMAT_PARAMETER_PATTERN;
    FORMAT_PARAMETER_PATTERN = /\@\(([a-zA-Z](?:[a-zA-Z0-9_]*))\)/g;
    String.prototype.format = function(parameters) {
      var value;
      value = this.valueOf();
      return value.replace(FORMAT_PARAMETER_PATTERN, function(match, name) {
        var _ref;
        return (_ref = parameters != null ? parameters[name] : void 0) != null ? _ref : "<>";
      });
    ...
    
  2. format(params)
    String.prototype.format = function(params) {
      if (!params) {throw new Error('No arguments passed');}
      return this.replace(/\{([a-zA-Z0-9]+)\}/g, function() {
        var key = arguments[1];
        if (params.hasOwnProperty(key)) {
          return params[key];
        } else {
          return arguments[0];
      });
    };
    
  3. format(params)
    String.prototype.format = function(params){
      var formatted = this;
      var fn = function(key, value){
        var regexp = new RegExp('\\{' + key + '\\}', 'gi');
        formatted = formatted.replace(regexp, value);
      };
      if (typeof(params) == 'object') {
        for (var x in params) {
          fn(x, params[x]);
    ...
    
  4. format(params)
    String.prototype.format = function (params) {
        'use strict';
        var p,
            regex,
            result;
        result = this;
        for (p in params) {
            if (params.hasOwnProperty(p)) {
                regex = new RegExp('#{' + p + '}', 'g');
    ...
    
  5. format(params)
    String.prototype.format = function (params){
        var reg = /{(\d+)}/gm;
        return this.replace(reg,function(match,name){
            return params[~~name];}
        );
    };
    
  6. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  7. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) { 
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  8. format(substitutions)
    'use strict';
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : ''
      })
    
  9. format(substitutions)
    String.prototype.format = function(substitutions) {
        'use strict';
        return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
            return substitutions[key] !== undefined ? substitutions[key] : '';
        });
    };