Nodejs String Format toFormattedString(f)

Here you can find the source of toFormattedString(f)

Method Source Code

Date.prototype.toFormattedString = function (f) {
    var nm = this.getMonthName();
    var nd = this.getDayName();
    f = f.replace(/yyyy/g, this.getFullYear());
    f = f.replace(/yy/g, String(this.getFullYear()).substr(2, 2));
    f = f.replace(/MMM/g, nm.substr(0, 3).toUpperCase());
    f = f.replace(/Mmm/g, nm.substr(0, 3));
    f = f.replace(/MM\*/g, nm.toUpperCase());
    f = f.replace(/Mm\*/g, nm);//from  ww  w  .j av  a 2 s  .  c om
    f = f.replace(/DDD/g, nd.substr(0, 3).toUpperCase());
    f = f.replace(/Ddd/g, nd.substr(0, 3));
    f = f.replace(/DD\*/g, nd.toUpperCase());
    f = f.replace(/Dd\*/g, nd);
    f = f.replace(/dd/g, String(this.getDate()).padLeft('0', 2));
    f = f.replace(/d\*/g, this.getDate());
    f = f.replace(/HH/g, this.getHours().padLeft('0', 2));
    f = f.replace(/mm/g, this.getMinutes().padLeft('0', 2));
    return f;
};

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

String.prototype.padLeft = function (value, size) {
    var x = this;
    while (x.length < size) { x = value + x; }
    return x;
};
Date.prototype.getMonthName = function () {
    return months[this.getMonth()];
};
Date.prototype.getDayName = function () {
    return days[this.getDay()];
};
Date.prototype.addSeconds = function (sec) {
    return new Date(this.getTime() + sec * 1000);
};
Number.prototype.toMMSS = function () {
    var sec_num = this; // don't forget the second param
    var hours = Math.floor(this / 3600);
    var minutes = Math.floor((this - (hours * 3600)) / 60);
    var seconds = Math.floor(this - (hours * 3600) - (minutes * 60));

    if (hours < 10) { hours = "0" + hours; }
    if (minutes < 10) { minutes = "0" + minutes; }
    if (seconds < 10) { seconds = "0" + seconds; }
    return (hours > 0 ? hours + ":" : "") + minutes + ':' + seconds;
};

Related

  1. format()
    String.prototype.format = String.prototype.format || function () {
      var string = this
      for (var i = 0, j = arguments.length; i < j; i++) {
        string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i])
      return string
    
  2. format()
    String.prototype.format = String.prototype.format || function format() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function($0, $1) {
            return args[+$1];
        });
    };
    
  3. format(object)
    String.prototype.format = function format(object) {
        let parsed = this;
        for (const option in object) {
            if ({}.hasOwnProperty.call(object, option)) {
                const regex = new RegExp(`#{${option}}`, 'g');
                parsed = parsed.replace(regex, object[option]);
        return parsed;
    ...
    
  4. format()
    function format () {
      if (arguments.length === 0) return this;
      var iter;
      if (arguments.length > 1) { 
        iter = arguments;
      } else {
        iter = arguments[0];
      var ret = this;
    ...
    
  5. format()
    (function(){
    function format () {
      if (arguments.length === 0) return this;
      var iter;
      if (arguments.length > 1) { 
        iter = arguments;
      } else {
        iter = arguments[0];
      var ret = this;
      for (var i in iter) {
        ret = ret.replace('{'+i+'}', iter[i]);
      return ret;
    String.prototype.format = format;
    })();
    
  6. toFormattedString(f)
    Date.prototype.toFormattedString = function(f) {
        f = f.replace(/yyyy/g, this.getFullYear());
        f = f.replace(/yy/g, String(this.getFullYear()).substr(2, 2));
        f = f.replace(/mm/g, String(this.getMonth() + 1).padLeft('0', 2));
        f = f.replace(/dd/g, String(this.getDate()).padLeft('0', 2));
        f = f.replace(/HH/g, String(this.getHours()).padLeft('0', 2));
        f = f.replace(/MM/g, String(this.getMinutes()).padLeft('0', 2));
        return f;
    };
    ...
    
  7. toFormattedString(format)
    WScript.echo("Date Extensions Module Loaded");
    Date.prototype.toFormattedString = function (format) {
        var year = (this.getFullYear()).toString();
        var month = (1 + this.getMonth()).toString();
        var day = (this.getDate()).toString();
        month = month.length > 1 ? month : "0" + month;
        day = day.length > 1 ? day : "0" + day;
        switch (format) {
            case "YYYYMMDD": return year + month + day; break;
    ...
    
  8. toFormattedString(include_time)
    Date.prototype.toFormattedString = function(include_time) {
      var str = this.getFullYear() + '-' + Date.padded2(this.getMonth() + 1) + '-' + Date.padded2(this.getDate());
      return str;
    
  9. toFormattedString(include_time)
    Date.prototype.toFormattedString = function(include_time)
       str = this.getFullYear() + "." + Date.padded2(this.getMonth()+1) + "." + Date.padded2(this.getDate());
       if (include_time) { str += " " + this.getHours() + ":" + this.getPaddedMinutes() }
       return str;