title: Checking the time difference 24 hours from now using Moment
body: <p>My goal is to build a feature like snapchat, 24 hours from now the content that I post will be deleted. I want to create an API that will check whether the current time has past 24 hours or not, if past then It will delete it, it will check the created time and the current time, and it will compare between these two time.</p>
  
  <p>I use moment to store dates/time to database. Let say that my database code looks like this</p>
  
  <pre><code>var mongoose = require('mongoose');
  var moment = require('moment');
  
  var ItemPost = new Schema({
     name: String,
     created: { type: Date, default: moment() },
     timeLeft: { type: Number, default: 24 },
  });
  </code></pre>
  
  <p>For example if I post a content right now</p>
  
  <pre><code>11:31 PM 8 Jan 2017
  </code></pre>
  
  <p>Assume that the time right now is of the above. Let say 9 hours has passed, and the current time is</p>
  
  <pre><code>08:31 PM 9 Jan 2017
  </code></pre>
  
  <p>How would I get the difference in time 24 hours from the created date?</p>
  
  <p>My attempt</p>
  
  <pre><code>function checkTime(item) {
    var currentTime = moment();
    // assume item is an ItemPost object from the database
    var timeStore = item.created()
  
    // Im stuck here, I tried various codes. But nothing has worked so Far.
    return data;
  
  }
  </code></pre>
  
  <p>The result that I want is </p>
  
  <pre><code>console.log(checkTime(item)); // 1 or 1 hour
  </code></pre>
  
  <p>My real attempt of getting close to my goal is </p>
  
  <pre><code>function test0() {
  
    var currentTime = moment();
    var timeStored = moment().subtract(7, 'hours');
  
    var hoursMinus = timeStored.fromNow(currentTime);
    console.log(hoursMinus); // 7 hours - it is a string
  
    timeDiff = hoursMinus.substr(0, hoursMinus.indexOf(' ')); // will get just 7
  
    var timeLeft = 24; // Default data from database
  
    console.log(timeLeft - parseInt(timeDiff));
  }
  </code></pre>
  
  <p>Well this solution work well, if I subtract <strong>7</strong> </p>
  
  <pre><code>var timeStored = moment().subtract(7, 'hours');
  </code></pre>
  
  <p>But it won't work if the time is more or equal than 23</p>
  
  <pre><code>var timeStored = moment().subtract(23, 'hours');
  var hoursMinus = timeStored.fromNow(currentTime);
  console.log(hoursMinus); // Will return "a day"
  </code></pre>
  
  <p>hoursMinus will return <code>a day</code> as the value which is bad for my algorithm</p>
  
  <p>My goal is to return a data that says <code>1 hour left</code> or <code>7 hours left</code> from the original created date. Any help would be appreciated.</p>