Nodejs Date Convert toFormat(format)

Here you can find the source of toFormat(format)

Method Source Code

Date.prototype.toFormat = function (format) {
  format = format.replace(/H/, this.getHours());
  format = format.replace(/m/, this.getMinutes());
  format = format.replace(/s/, this.getSeconds());

  return format;/* w w w  . jav a 2s  . co m*/
};

Related

  1. toDateOrTimeStr()
    Date.prototype.toDateOrTimeStr = function() {
      if (this.isToday()){
        var dt = this.toLocaleTimeString();
        return dt.slice(0, -6) + dt.slice(-3); 
      } else
        return this.toLocaleDateString();
    
  2. toDateStr()
    function _pad(n, size){
      return ('00000' + n).slice(-size);
    Date.prototype.toDateStr = function() {
      return this.getFullYear() 
        + '-' + _pad((this.getMonth()+1), 2)
        + '-' + _pad(this.getDate(), 2);
    };
    Date.prototype.toTimeStr = function() {
    ...
    
  3. toDateTimeFormat()
    Date.prototype.toDateTimeFormat = function() {
      var year = this.getFullYear().toString();
      var month = (this.getMonth() + 1).toString();
      var day = this.getDate().toString();
      var hour = this.getHours().toString();
      var minute = this.getMinutes().toString();
      var second = this.getSeconds().toString();
      return year + '-' + (month[1] ? month : '0'+month[0]) + '-'+ (day[1] ? day : '0'+day[0]) + ' ' + (hour[1] ? hour : '0'+hour[0]) + ':' + (minute[1] ? minute : '0'+minute[0]) + ':' + (second[1] ? second : '0'+second[0]);
    
  4. toEndTimeInputValue(()
    Date.prototype.toEndTimeInputValue = (function() {
      var local = new Date(this);
      local.setMinutes(this.getMinutes() - this.getTimezoneOffset() + 60);
      return local.toJSON().slice(11,13) + ":00:00";
    });
    
  5. toFixedDate()
    Date.prototype.toFixedDate = function(){
      var months = [
        "January", "February", "March",
        "April", "May", "June",
        "Sol",
        "July", "August", "September",
        "October", "November", "December"
      ]
      var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday",
    ...
    
  6. toISO8601()
    Date.prototype.toISO8601 = function() {
      function pad(n) {
        return n < 10 ? '0' + n : n;
      return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours())
       + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + 'Z';
    };
    
  7. toISO8601()
    Date.prototype.toISO8601 = function() {    
        var s = [
              this.getFullYear()
            , Date.padZero(this.getMonth() + 1)
            , Date.padZero(this.getDate())
        ].join("-") + "T";
        s += this.getTimeString();
        s += this.getTimezoneOffsetString(true);
        return s;
    ...
    
  8. toISO8601(key)
    Date.prototype.toISO8601 = function (key) {
        function f(n) {
            return n < 10 ? '0' + n : n;
        function f2(n) {
            if(n < 10)
              return '00' + n;
            return n < 100 ? '0' + n : n;
        return isFinite(this.valueOf()) ?
               this.getUTCFullYear()   + '-' +
             f(this.getUTCMonth() + 1) + '-' +
             f(this.getUTCDate())      + 'T' +
             f(this.getUTCHours())     + ':' +
             f(this.getUTCMinutes())   + ':' +
             f(this.getUTCSeconds())   + '.' +
             f2(this.getUTCMilliseconds()) + '-0000' : null;
    };
    
  9. toInputFormat()
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); 
       var dd  = this.getDate().toString();
       return yyyy +'-'+ (mm[1]?mm:"0"+mm[0]) +'-'+ (dd[1]?dd:"0"+dd[0]); 
      };