Nodejs Date Convert toRelativeTime(now_threshold)

Here you can find the source of toRelativeTime(now_threshold)

Method Source Code

/**//from   w w  w  .j  av a2 s.c o m
 * Javascript Relative Time Helper
 * The MIT License
 *
 * Copyright (c) 2009 James F. Herdman 
 * https://github.com/jherdman/javascript-relative-time-helpers
 *
 * from http://stackoverflow.com/questions/3764459/why-will-this-dateparser-not-work-in-safari
 *
 * Returns a description of this past date in relative terms.
 * Takes an optional parameter (default: 0) setting the threshold in ms which
 * is considered "Just now".
 *
 * Examples, where new Date().toString() == "Mon Nov 23 2009 17:36:51 GMT-0500 (EST)":
 *
 * new Date().toRelativeTime()
 * --> 'Just now'
 *
 * new Date("Nov 21, 2009").toRelativeTime()
 * --> '2 days ago'
 *
 * // One second ago
 * new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime()
 * --> '1 second ago'
 *
 * // One second ago, now setting a now_threshold to 5 seconds
 * new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime(5000)
 * --> 'Just now'
 *
 */
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 (Math.abs(delta) <= now_threshold) {
    return 'Just now';
  }

  var units = null;
  var conversions = {
    millisecond: 1, // ms    -> ms
    second: 1000,   // ms    -> sec
    minute: 60,     // sec   -> min
    hour:   60,     // min   -> hour
    day:    24,     // hour  -> day
    month:  30,     // day   -> month (roughly)
    year:   12      // month -> year
  };

  for (var key in conversions) {
    if (Math.abs(delta) < conversions[key]) {
      break;
    } else {
      units = key; // keeps track of the selected key over the iteration
      delta = delta / conversions[key];
    }
  }

  // pluralize a unit when the difference is greater than 1.
  delta = delta > 0 ? Math.floor(delta) : Math.ceil(delta);
  if (Math.abs(delta) !== 1) { units += "s"; }

  if (delta > 0) {
    return [delta, units, "ago"].join(" ");
  } else {
    return ["in", -delta, units].join(" ");
  }
};

/*
 * Wraps up a common pattern used with this plugin whereby you take a String
 * representation of a Date, and want back a date object.
 */
Date.fromString = function(str) {
  return new Date(Date.parse(str));
};

Related

  1. toRails(name)
    Date.prototype.toRails = function(name) {
       var dateParams;
       dateParams = {};
       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;
    ...
    
  2. toReadableTime()
    Date.prototype.toReadableTime = function() {
      if(this.getHours()>0) {
        return this.getHours()+"h "+this.getMinutes()+" min"
      } else {
        return this.getMinutes()+" min"
    
  3. 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));
    };
    
  4. 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,     
    ...
    
  5. 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(" ");
    };
    
  6. toShortTime()
    Date.prototype.toShortTime = function() {
        return ('0' + this.getHours()).slice(-2) + ':' + ('0' + this.getMinutes()).slice(-2);
    
  7. toSource()
    Date.prototype.toSource = function(){
      return '(new Date(' + this.valueOf() + '))';
    };
    
  8. toStartTimeInputValue(()
    Date.prototype.toStartTimeInputValue = (function() {
      var local = new Date(this);
      local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
      return local.toJSON().slice(11,13) + ":00:00";
    });
    
  9. toText()
    Date.prototype.toText=function(){ 
      var m; 
      var d; 
      if(this.getMonth()<9){ 
      m="0"+(this.getMonth()+1); 
      }else{ 
      m=this.getMonth()+1; 
      if(this.getDate()<10){ 
    ...