-2

Does someone know how can I get in hours not days, this code below returns 3 days, I would like to return 72 hours

console.log(moment("2022-04-14T09:07:08.086Z").endOf('hours').fromNow(true))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
lilo
  • 185
  • 1
  • 4
  • 17
  • 2
    If you're going to add a snippet to the question, please make sure it works! If it's not meant to be runnable then don't use snippets – phuzi Apr 11 '22 at 09:54
  • 2
    Does this answer your question? [Moment.js how to use fromNow() to return everything in hours?](https://stackoverflow.com/questions/41508796/moment-js-how-to-use-fromnow-to-return-everything-in-hours) As a side note - momentjs is deprecated. – eol Apr 11 '22 at 09:58

1 Answers1

0

const toHours = (days) => { days = days.substring(0, days.indexOf(' '))
                          return 24*days
}

console.log(toHours(moment("2022-04-14T09:07:08.086Z").fromNow(true)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
OR use relative threshold by moment (sometimes incorrect due to rounding off)
moment.relativeTimeThreshold('h', 24*26);

console.log(moment("2022-04-14T09:07:08.086Z").endOf('hours').fromNow(true))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Ritik Banger
  • 2,107
  • 5
  • 25