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? 😔
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)
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