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

119
Views
Difference between hours using moment

I understand this is a popular question on the internet. But the point is that I need to do something accurately and as I'm not very familiar with working with dates, I needed to ask this question.

My problem is the following, from a timestamp I have to define certain actions. These actions are taken under the following conditions:

Prep - starts about an hour before timestamp;

Run - starts right at timestamp;

Reset - this action is taken if 15 minutes have passed after the timestamp;

Bearing in mind that the timestamp would be the following:

const timestamp = "2021-11-16T09:18:02+0000"

I had the idea to do it this way:

const time = moment(timestamp).diff(moment(), "minutes")

But to be honest I don't know how I can improve this. Can you help? ๐Ÿ˜”

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

No need for moment

this should help you get started

const timestamp = "2021-11-16T09:18:02+0000"

const d = new Date(timestamp)

const prep = new Date(timestamp);
prep.setHours(prep.getHours()-1);// Prep - starts about an hour before timestamp;

// Run - starts right at timestamp;


const resetTime = new Date(timestamp)
// Reset - this action is taken if 15 minutes have passed after the timestamp;
resetTime.setMinutes(resetTime.getMinutes()-15)

console.log(d)
console.log(prep)
console.log(resetTime)

console.log((d.getTime()-prep.getTime())/60000)

about 4 years ago ยท Juan Pablo Isaza Report

0

This is the solution I found suitable using momentjs

const timestamp = "2021-11-16T09:18:02+0000"
const reset = moment(timestamp).add(15, 'minutes').format('LT')
const current = moment(timestamp).format('LT')
const prev = moment(timestamp).subtract(1, 'hour').format('LT')

console.log("Reset:",reset,"\nCurrent",current,"\nPrev",prev)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Which when logged out

reset 3:03 PM current 2:48 PM prev 1:48 PM

For More Regarding momentjs

about 4 years ago ยท Juan Pablo Isaza 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!