Nodejs Date Convert toDateStr()

Here you can find the source of toDateStr()

Method Source Code

/*//from  ww w. ja v a  2 s .c om
 * Power up the Date prototype with some toString functions
 */

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() {
   return _pad(this.getHours(), 2)
      + ':' + _pad(this.getMinutes(), 2)
      + ':' + _pad(this.getSeconds(), 2);
};

Date.prototype.toDateAndTimeStr = function() {
   return this.toDateStr() + ' ' + this.toTimeStr();
};

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

Related

  1. toDateInputValue(()
    Date.prototype.toDateInputValue = (function() {
        var local = new Date(this);
        local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
        return local.toJSON().slice(0, 10);
    });
    
  2. toDateInputValue(()
    Date.prototype.toDateInputValue = (function() {
      var local = new Date();
      return local.toJSON().slice(0,10);
    });
    
  3. toDateInputValue()
    'use strict';
    Date.prototype.toDateInputValue = function() {
        var local = new Date(this);
        local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
        return local.toJSON().slice(0,10);
    };
    
  4. toDateOrDefault(input, defaultDate)
    Date.toDateOrDefault = function(input, defaultDate){
      return (typeof input == "string") ? Date.fromJSON(input) : input || defaultDate || new Date(); 
    };
    
  5. toDateOrTimeStr()
    Date.prototype.toDateOrTimeStr = function() {
      if (this.isToday()){
        var dt = this.toLocaleTimeString();
        return dt.slice(0, -6) + dt.slice(-3); 
      } else
        return this.toLocaleDateString();
    
  6. 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]);
    
  7. 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";
    });
    
  8. 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",
    ...
    
  9. toFormat(format)
    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;
    };