Nodejs Date Convert toFixedDate()

Here you can find the source of toFixedDate()

Method Source Code

Date.prototype.toFixedDate = function(){
  var months = [/*from w w  w  .j a  v  a  2 s  .co  m*/
    "January", "February", "March",
    "April", "May", "June",
    "Sol",
    "July", "August", "September",
    "October", "November", "December"
  ]
  var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday",
              "Thursday", "Friday", "Saturday"]
  var fd = {year: this.getFullYear()}
  var days = this.getDayOfYear();
  var month = Math.floor((days-1) / 28);
  var date = days%28;
  if (date == 0) {
    date = 28;
  }
  fd.day = daysOfWeek[(date-1)%7];
  var leapYear = isLeapYear(this.getFullYear());
  if (leapYear){
    if (days == 169){
      // special leap day case
      fd.monthName = "Leap Day";
      fd.date = 0;
      fd.isSpecial = true;
      return fd;
    }
    else if (days > 169) {
      //subtract leap day
      date = (days-1) % 28;
      if (date == 0) {
        date = 28;
      }
      month = Math.floor((days-2) / 28);
    }
    if (days == 366){
      fd.monthName = "Year Day";
      fd.date = 0;
      fd.isSpecial = true;
      return fd;
    }
  }
  else if (days == 365){
    //day 365 is "year day"
    fd.monthName = "Year Day";
    fd.date = 0;
    fd.isSpecial = true;
    return fd;
  }
  fd.monthName = months[month];
  fd.date = date;
  return fd;
}

Related

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