title: Moment js determine how many days, weeks has passed
body: <p>I'm not really good at handling <strong>datetime</strong>. I used <a href="https://momentjs.com/docs/" rel="nofollow noreferrer">momentjs</a> for a <strong>ionic app</strong> for manipulating time but I want to achieve something that I couldn't.</p>
  
  <p>I used a pipe for that and I want to display based on if how many days have been passed or if weeks and months or years. Using <strong>relative time</strong> would help me that like the <code>fromNow()</code> method and the <code>calendar()</code> of <strong>momentjs</strong>. But in my case I would have multiple <code>conditions</code>.</p>
  
  <p>Here is the sample code below of my pipe</p>
  
  <pre><code>transform(value: Date | moment.Moment, dateFormat: string): any {
      if (moment(value) &lt; moment(value).subtract(7, 'days')) {
        return moment(value).format('llll') // Use this format if weeks, months or years has passed
      } else if (moment(value) &lt; moment(value).subtract(1, 'days')) {
        return moment(value).calendar(); // Use calendar time if 1 day has passed
      } else {
        return moment(value).fromNow(); // Use relative time if within 24 hours
      }
    }
  </code></pre>
  
  <p>If seconds, minutes or hours has passed until 24 hours I will use the <code>fromNow()</code> method but when days passed I will use the <code>calendar()</code> and if weeks, months or years passed use this <code>format('llll')</code>.</p>
  
  <p>Can someone shed some light for me?</p>
  
  <p>Thanks in advance.</p>