Nodejs Date Format format()

Here you can find the source of format()

Method Source Code

Date.prototype.format = function(){
   var formatMonth = function(month, numberOfDigits){
      month += 1;/*w  ww.j a v  a 2s  . c  o m*/
      month = insertZerosLeft(month, numberOfDigits);
      return month;
   };

   var insertZerosLeft = function(data, numberOfDigits){
      data = data.toString();
      var zeros = '';
      var qtdZerosLeft = numberOfDigits - data.length;

      for(var count = 0; count < qtdZerosLeft; count++){
         zeros += '0';
      }

      data = zeros + data;
      return data;
   };

   var dateFormated = [];
   dateFormated.push( this.getFullYear() );
   dateFormated.push( formatMonth(this.getMonth(), 2) );
   dateFormated.push( insertZerosLeft(this.getDate(), 2) );

   var timeFormated = [];
   timeFormated.push( insertZerosLeft(this.getHours(), 2) );
   timeFormated.push( insertZerosLeft(this.getMinutes(), 2) );
   timeFormated.push( insertZerosLeft(this.getSeconds(), 2) );

   var datetimeFomated = dateFormated.join('-');
   datetimeFomated += ' ';
   datetimeFomated += timeFormated.join(':');

   return datetimeFomated;
};

Related

  1. format(()
    Date.prototype.format = (function() {
      var
        that, txts,
        roman = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'],
        addZero = function(num) {
          return num < 10 ? '0' + num : num;
        },
        na = function(n, z) {
          return n % z;
    ...
    
  2. format()
    Date.prototype.format = function () {
      'use strict';
      var
          d = this.getDate()
        , m = this.getMonth() + 1
        , y = this.getFullYear();
        return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
    };
    
  3. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...
    
  4. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...
    
  5. format()
    Date.prototype.format = function() {
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth();
      var curr_year = d.getFullYear();
      return (curr_date + "/" + curr_month + "/" + curr_year);
    
  6. format()
    Date.prototype.format = function () {
        var month = this.getMonth() + 1;
        var date = this.getDate();
        month < 10 && (month = "0" + month);
        date < 10 && (date = "0" + date);
        return [this.getFullYear(), month, date].join("-");
    };
    
  7. format(dateFormat)
    Date.prototype.format = function (dateFormat) {
        var day = this.getDate();
        var month = this.getMonth() + 1;
        var year = this.getFullYear();
        if (dateFormat === "dd/mm/yyyy")
            return '{0}/{1}/{2}'.format(day, month > 9 ? month : '0' + month, year);
    };
    ...
    
  8. format(f)
    Date.prototype.format = function(f) {
        if (!this.valueOf()) return " ";
        var weekName = ["?", "?", "?", "?", "?", "?", "?"];
        var d = this;
        return f.replace(/(yyyy|yy|MM|dd|E|hh|mm|ss|a\/p)/gi, function($1) {
            switch ($1) {
                case "yyyy": return d.getFullYear();
                case "yy": return (d.getFullYear() % 1000).zf(2);
                case "MM": return (d.getMonth() + 1).zf(2);
    ...
    
  9. format(f)
    var gsMonthNames = new Array(
    'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
    );
    var gsDayNames = new Array(
    'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
    );
    Date.prototype.format = function (f) {
        if (!this.valueOf())
            return ' ';
    ...