Nodejs Date Convert dateToFormat(format)

Here you can find the source of dateToFormat(format)

Method Source Code

Date.prototype.dateToFormat = function(format) {
    var date = this.dateToStr();
    var yil = date.slice(0, 4);
    var ay = date.slice(4, 6);
    var gun = date.slice(6, 8);
    var saat = date.slice(8, 10);
    var dakika = date.slice(10, 12);
    var result = "";
    /*from   w  w  w. j  av  a2s .c  o m*/
    for (var i = 0; i < format.length; i++) {
        switch (format[i]) {
            case "y":
               result += yil;
                break;
            case "M":
               result += ay;
                break;
            case "d":
               result += gun;
                break;
            case "H":
               result += saat;
                break;
            case "m":
               result += dakika;
                break;
           default : 
              result += format[i];
        }
    }
    return result;
};
String.prototype.dateToFormat = function(format) {
   return this.strToDate().dateToFormat(format);
};

Related

  1. dateToJSON()
    Date.prototype.dateToJSON = function() {
      return this.toJSON().substring(0, 11);
    };
    
  2. dateToStr()
    Date.prototype.dateToStr = function() {
        var yil = this.getUTCFullYear();
        var ay = parseInt((this.getMonth() + 1) / 10) == 0 ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1);
        var gun = parseInt(this.getDate() / 10) == 0 ? ("0" + this.getDate()) : this.getDate();
        var saat = parseInt(this.getHours() / 10) == 0 ? ("0" + this.getHours()) : this.getHours();
        var dakika = parseInt(this.getMinutes() / 10) == 0 ? ("0" + this.getMinutes()) : this.getMinutes();
        return String(yil + ay + gun + saat + dakika);
    
  3. dateToYMD()
    Date.prototype.dateToYMD = function() {
        var d = this.getDate();
        var m = this.getMonth() + 1;
        var y = this.getFullYear();
        return  y + (m<=9 ? '0' + m : m) +  (d <= 9 ? '0' + d : d);
    
  4. toAmPm()
    Date.prototype.toAmPm = function () {
        var hours = this.getHours();
        var minutes = this.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        return hours + ':' + minutes + ampm;
    };
    ...