Nodejs Date Convert toRails(name)

Here you can find the source of toRails(name)

Method Source Code

Date.prototype.toRails = function(name) {
   var dateParams;
   dateParams = {};//www. j  a  v a  2s.com
   dateParams[name + "(1i)"] = this.getFullYear();
   dateParams[name + "(2i)"] = this.getMonth() + 1;
   dateParams[name + "(3i)"] = this.getDate();
   dateParams[name + "(4i)"] = this.getHours();
   dateParams[name + "(5i)"] = this.getMinutes();
   return dateParams;
 };


//take top N entries of list
//add to route_list
//two options of moving list items
Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

Related

  1. toLocaleJSON()
    Date.prototype.toLocaleJSON = function() {
      var t = this,
        y = t.getFullYear(),
        M = t.getMonth() + 1,
        d = t.getDate(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds();
      return y + '/' + (M < 10 ? '0' + M : M) + '/' + (d < 10 ? '0' + d : d) + ' ' +
    ...
    
  2. toLongintText()
    Date.prototype.toLongintText=function(){ 
      var m; 
      var d; 
      if(this.getMonth()<9){ 
      m="0"+(this.getMonth()+1); 
      }else{ 
      m=this.getMonth()+1; 
      if(this.getDate()<10){ 
    ...
    
  3. toOADate()
    Date.prototype.toOADate = function ()
      var jsDate = this || new Date();
      var timezoneOffset = jsDate.getTimezoneOffset() / (60 * 24);
      var msDateObj = (jsDate.getTime() / 86400000) + (25569 - timezoneOffset);
      return msDateObj;
    
  4. toRFC2822()
    Date.prototype.toRFC2822 = function() {    
        return [
              Date.DAYS[this.getDay()] + ","
            , Date.padZero(this.getDate())
            , Date.MONTHS[this.getMonth()]
            , this.getFullYear()
            , this.getTimeString()
            , this.getTimezoneOffsetString()
        ].join(" ");
    ...
    
  5. toRFC3339()
    Date.prototype.toRFC3339 = function()
      var year = this.getFullYear();
      var month = this.getMonth()+1;  
      var date = this.getDate();
      var hours = this.getHours();
      var mins = this.getMinutes();
      var secs = this.getSeconds();
      return (year+'-'+month+'-'+date+' '+hours+':'+mins+':'+secs);
    ...
    
  6. toReadableTime()
    Date.prototype.toReadableTime = function() {
      if(this.getHours()>0) {
        return this.getHours()+"h "+this.getMinutes()+" min"
      } else {
        return this.getMinutes()+" min"
    
  7. toRelativeTime( now_threshold )
    Date.prototype.toRelativeTime = function( now_threshold ) {
      var delta = new Date() - this;
      now_threshold = parseInt(now_threshold, 10);
      if (isNaN(now_threshold)) {
        now_threshold = 0;
      if (delta <= now_threshold) {
        return 'Just now';
      var units = null;
      var conversions = {
          millisecond : 1, 
          second : 1000, 
          minute : 60, 
          hour : 60, 
          day : 24, 
          month : 30, 
          year : 12
      };
      for ( var key in conversions) {
        if (delta < conversions[key]) {
          break;
        else {
          units = key; 
          delta = delta / conversions[key];
      delta = Math.floor(delta);
      if (delta !== 1) {
        units += "s";
      return [ delta, units ].join(" ");
    };
    Date.fromString = function( str ) {
      return new Date(Date.parse(str));
    };
    
  8. toRelativeTime()
    Date.prototype.toRelativeTime = function() {
      var delta       = new Date() - this;
      var units       = null;
      var conversions = {
        millisecond: 1, 
        second: 1000,   
        minute: 60,     
        hour:   60,     
        day:    24,     
    ...
    
  9. toRelativeTime(now_threshold)
    Date.prototype.toRelativeTime = function(now_threshold) {
      var delta = new Date() - this;
      now_threshold = parseInt(now_threshold, 10);
      if (isNaN(now_threshold)) {
        now_threshold = 0;
      if (delta <= now_threshold) {
        return 'Just now';
      var units = null;
      var conversions = {
        millisecond: 1, 
        second: 1000,   
        minute: 60,     
        hour:   60,     
        day:    24,     
        month:  30,     
        year:   12      
      };
      for (var key in conversions) {
        if (delta < conversions[key]) {
          break;
        } else {
          units = key; 
          delta = delta / conversions[key];
      delta = Math.floor(delta);
      if (delta !== 1) { units += "s"; }
      return [delta, units, "ago"].join(" ");
    };