Nodejs Date Format formatDate(format)

Here you can find the source of formatDate(format)

Method Source Code

Date.prototype.formatDate = function (format) {
    var date = this,
        day = date.getDate(),//from ww w .  j  a  v a2s. c o  m
        month = date.getMonth() + 1,
        year = date.getFullYear(),
        hours = date.getHours(),
        minutes = date.getMinutes(),
        seconds = date.getSeconds();

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

    format = format.replace("MM", month.toString().replace(/^(\d)$/, '0$1'));

    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().replace(/^(\d)$/, '0$1'));

    if (format.indexOf("t") > -1) {
        if (hours > 11) {
            format = format.replace("t", "pm");
        } else {
            format = format.replace("t", "am");
        }
    }

    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("mm") > -1) {
        format = format.replace("mm", 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. format1()
    Date.prototype.format1 = function() {
        return "" + this.getMonth() + this.getFullYear();
    function f(d) {
        for (var i=0; i<60; i++) {
            assertEq(d.format1(), "91987");
    f(new Date("10/10/1987 1:11:11"));
    ...
    
  2. formatAMPM(date)
    function formatAMPM(date) {
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var ampm = hours >= 12 ? 'pm' : 'am';
      hours = hours % 12;
      hours = hours ? hours : 12; 
      minutes = minutes < 10 ? '0'+minutes : minutes;
      var strTime = hours + ':' + minutes + ' ' + ampm;
      return strTime;
    ...
    
  3. formatDate(date)
    function formatDate(date) {
      if(date instanceof Date === false) {
        return console.log("err");
      var year = date.getFullYear();
      var month = pad(date.getMonth() + 1);
      var day = pad(date.getDate());
      return [year, month, day].join('-');
    
  4. formatDate(date)
    function formatDate(date){
      return date.split('-').reverse().join('/');
    
  5. formatDate(format)
    Date.prototype.formatDate = function(format) {
      var date = this,
        day = date.getDate(),
        month = date.getMonth() + 1,
        year = date.getFullYear(),
        hours = date.getHours(),
        minutes = date.getMinutes(),
        seconds = date.getSeconds();
      if (!format) {
    ...
    
  6. formatDate(format)
    Date.prototype.formatDate = function(format) {
      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() + 1;
      var mm = m < 10 ? "0" + m : m;
      var mmm = months[m - 1];
      var d = this.getDate();
      var dd = d < 10 ? "0" + d : d;
    ...
    
  7. formatDay(pattern, dayNames = [], monthnames = [])
    Date.prototype.formatDay = function (pattern, dayNames = [], monthnames = []) {
        pattern = pattern.replace(/dddd/g, dayNames[this.getDay()]);
        pattern = pattern.replace(/dd/g, this.getDate().toString().padLeft(2, 0));
        pattern = pattern.replace(/mmm/g, monthnames[this.getMonth()]);
        pattern = pattern.replace(/mm/g, (this.getMonth() + 1).toString().padLeft(2, 0));
        pattern = pattern.replace(/yyyy/g, this.getFullYear().toString());
        pattern = pattern.replace(/yy/g, this.getFullYear().toString().substr(2, 2));
        return pattern;
    };
    ...
    
  8. formatMMDD()
    Date.prototype.formatMMDD = function () {
       return this.getMonth() + 1 + '/' + this.getDate();
    };
    
  9. formatMMDDYYYY()
    Date.prototype.formatMMDDYYYY = function () {
       return this.getMonth() + 1 + '/' + this.getDate() + '/' + this.getFullYear();
    };