4

Actually I have a date time or time-stamp format looks like 2015-12-18 07:10:54 this time should be converted or modified by like 2 hours ago or else.

I have tried this one. but there is something wrong with this.

Always showing 8 days ago. {which is wrong estimation.}

function ( date ) {

    var seconds = Math.floor(( new Date() - date ) / 1000 );

    var interval = Math.floor( seconds / 31536000 );

    if ( interval == 1 ) {

        return interval + " year ago";
    }
    if ( interval > 1 ) {

        return interval + " years ago";
    }

    interval = Math.floor( seconds / 2592000 );
    if ( interval == 1 ) {

        return interval + " month ago";
    }
    if ( interval > 1 ) {

        return interval + " months ago";
    }

    interval = Math.floor( seconds / 86400 );
    if ( interval == 1 ) {

        return interval + " day ago";
    }
    if ( interval > 1 ) {

        return interval + " days ago";
    }

    interval = Math.floor( seconds / 3600 );
    if ( interval == 1 ) {

        return interval + " hour ago";
    }
    if ( interval > 1 ) {

        return interval + " hours ago";
    }

    interval = Math.floor( seconds / 60 );
    if ( interval == 1 ) {

        return interval + " minute ago";
    }
    if ( interval > 1 ) {

        return interval + " minutes ago";
    }

    return Math.floor(seconds) + " seconds ago";
}
iam
  • 973
  • 4
  • 11
  • 21

4 Answers4

7

I can suggest to use MomentJS that's a powerful library to manage dates, install it with :

npm install --save moment

And then you simply get the time from now as:

moment().fromNow();

Or

var yourDate = new Date();
moment(yourDate).fromNow();

Or as per your example:

moment("2015-12-18 07:10:54", "YYYY-MM-DD HH:mm:ss").fromNow();

If you have a date in ISO format like that below, you don't need to format it, just use it:

moment("2015-12-09T12:09:30.000Z").fromNow()
michelem
  • 14,430
  • 5
  • 50
  • 66
  • thats preety good. but giving me Invalid Date. Actually In my database time is saved as and if i fetch that time , then it print look like 2015-12-09T12:09:30.000Z , so how to format this date?? :o – iam Dec 18 '15 at 09:54
  • You don't need to format that date because it's the standard one so just do: `moment("2015-12-09T12:09:30.000Z").fromNow()` Output: `9 days ago` – michelem Dec 18 '15 at 09:55
  • 1
    view : 2015-12-09T12:09:30.000Z but database : 2015-12-18 07:10:54 , how it is possible? – iam Dec 18 '15 at 10:01
0

Using MomentJS this kind of operation become simple as:

moment().subtract(10, 'days').calendar(); // 12/08/2015
moment().subtract(6, 'days').calendar();  // Last Saturday at 10:10 AM
moment().subtract(3, 'days').calendar();  // Last Tuesday at 10:10 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 10:10 AM
moment().calendar();                      // Today at 10:10 AM
moment().add(1, 'days').calendar();       // Tomorrow at 10:10 AM
moment().add(3, 'days').calendar();       // Monday at 10:10 AM
moment().add(10, 'days'
Luis González
  • 3,199
  • 26
  • 43
0

this time should be converted or modified by like 2 hours ago or else

var result = document.querySelector('#result');
var dateFromString = new Date('2015-12-18 07:10:54');

result.textContent = 'dateFromString: ' + dateFromString;

// dateFromString: travel 2 hours back
dateFromString.setHours(dateFromString.getHours()-2);
result.textContent += '\n2 hours before 2015-12-18 07:10:54: ' + 
                       dateFromString;


// determine difference between 2015-12-18 07:10:54 and the (time traveled) dateFromString
var difference = dateDiffCalc([new Date('2015-12-18 07:10:54'), dateFromString])
result.textContent += '\nHours difference dateFromString and '+
                      '2015-12-18 07:10:54: ' +
                      difference.hours;

function dateDiffCalc(value) {
  var ms = Math.abs(value[0].getTime()-value[1].getTime());
  var secsdf = Math.floor(((ms/1000)%(60*60))%60);
  var mindf  = Math.floor((ms/(60*1000))%60);
  var hourdf = Math.floor(ms/(60*1000*60)%24);
  var daydf  = Math.round((ms/(60*1000*60))/24);
  return {
    days: daydf,
    hours: hourdf,
    minutes: mindf,
    seconds: secsdf
  };
}
<pre id="result"></pre>
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • I'd agree moment for this use-case is far too heavy, something like this is far more workable for this problem. This solution does require a proper JS timestamp however, and the question does not show that that is a given. It looks like the actual problem is parsing from an irregular timestamp into one that JS understands. I'm not sure moment helps there either. Getting a proper timestamp should be the correct answer to this problem. – Matt Styles Dec 18 '15 at 09:42
0

As others have suggested, by using the Moment API you can achieve that very easily.

// Get the Time Since Content was Posted with Moment.js
  const getDate = (date) => {
    const dateTime = new Date(date * 1000);
    return moment(dateTime).fromNow();
  };

date is the UTC timestamp

Nima
  • 996
  • 2
  • 9
  • 37