Nodejs Date Convert toTurkishFormatDate(format)

Here you can find the source of toTurkishFormatDate(format)

Method Source Code

/*/*from   w w  w . j a v  a2  s. c o  m*/
 Based on fanatikhamsi code https://github.com/fanatikhamsi/
 Contributors:
 Selim Uban - @fanatikhamsi
 ------------------
 ?nek:
    var simdikiTarih = new Date();

    simdikiTarih.toTurkishFormatDate("dd.mm.yyyy")
    ?kti: 14.02.2016

    simdikiTarih.toTurkishFormatDate("dd MM DD yyyy")
    ?kti: 14 Subat Pazar 2016

    simdikiTarih.toTurkishFormatDate("dd MM yyyy")
    ?kti: 14 Subat 2016
 */
Date.prototype.toTurkishFormatDate = function(format) {
    var date = this,
            day = date.getDate(),
            weekDay = date.getDay(),
            month = date.getMonth() + 1,
            year = date.getFullYear(),
            hours = date.getHours(),
            minutes = date.getMinutes(),
            seconds = date.getSeconds();

    var monthNames = new Array("Ocak", "Subat", "Mart", "Nisan", "Mayis", "Haziran", "Temmuz", "Agustos", "Eyl?Ekim", "Kasim", "Aralik");
    var dayNames = new Array("Pazar", "Pazartesi", "Sali", "orsamba", "Persembe", "Cuma", "Cumartesi");

    if (!format) {
        format = "dd.MM.yyyy";
    }


    format = format.replace("mm", month.toString().padL(2, "0"));

    format = format.replace("MM", monthNames[month]);

    if (format.indexOf("yyyy") > -1) {
        format = format.replace("yyyy", year.toString());
    } else if (format.indexOf("yy") > -1) {
        format = format.replace("yy", year.toString().substr(2, 2));
    }

    format = format.replace("dd", day.toString().padL(2, "0"));
    
    format = format.replace("DD", dayNames[weekDay]);

    if (format.indexOf("HH") > -1) {
        format = format.replace("HH", hours.toString().replace(/^(\d)$/, '0$1'));
    }

    if (format.indexOf("hh") > -1) {
        if (hours > 12) {
            hours -= 12;
        }

        if (hours === 0) {
            hours = 12;
        }
        format = format.replace("hh", hours.toString().replace(/^(\d)$/, '0$1'));
    }

    if (format.indexOf("ii") > -1) {
        format = format.replace("ii", minutes.toString().replace(/^(\d)$/, '0$1'));
    }

    if (format.indexOf("ss") > -1) {
        format = format.replace("ss", seconds.toString().replace(/^(\d)$/, '0$1'));
    }

    return format;
};

Related

  1. toSource()
    Date.prototype.toSource = function(){
      return '(new Date(' + this.valueOf() + '))';
    };
    
  2. toStartTimeInputValue(()
    Date.prototype.toStartTimeInputValue = (function() {
      var local = new Date(this);
      local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
      return local.toJSON().slice(11,13) + ":00:00";
    });
    
  3. toText()
    Date.prototype.toText=function(){ 
      var m; 
      var d; 
      if(this.getMonth()<9){ 
      m="0"+(this.getMonth()+1); 
      }else{ 
      m=this.getMonth()+1; 
      if(this.getDate()<10){ 
    ...
    
  4. toTime()
    Date.prototype.toTime = function () {
        var hours = this.getHours();
        var minutes = this.getMinutes();
        if (minutes < 10)
            minutes = "0" + minutes;
        if (hours < 10)
            hours = "0" + hours;
        return hours + ':' + minutes;
    };
    ...
    
  5. toTimeInputValue(()
    Date.prototype.toTimeInputValue = (function() {
      var local = new Date(this);
      local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
      return local.toJSON().slice(11,19);
    });
    
  6. toUniversalDate()
    Date.prototype.toUniversalDate = function() {
        var localTime = this.getTime();
        var localOffset = this.getTimezoneOffset() * 60000;
        return new Date(localTime + localOffset);
    };
    
  7. toUnixTime()
    Date.prototype.toUnixTime = function() {
        return Math.round(this.getTime() / 1000);
    };
    
  8. toYYYYMMDD()
    Date.prototype.toYYYYMMDD = function() {
        return this.getFullYear() + String(this.getMonth()+1).lpad(2, "0") + String(this.getDate()).lpad(2, "0");
    
  9. toYmdHis()
    Date.prototype.toYmdHis = function() {
        return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds());
    };