Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
moment js end of from now return only in hours

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>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!