heres a photo of the snippetI want to create a prayer app where it tells you how much time is left between current time and the time of the prayer
I tried to find the difference directly by doing this
prayers = [[13,59],
[13,30]];
let time = new Date();
let currprayer = new Date();
for (let index in prayers) {
currprayer.setHours(prayers[index][0])
currprayer.setMinutes(prayers[index][1])
if (currprayer.getTime() > time.getTime()){
currprayer.setSeconds(00)
let left = new Date(currprayer.getTime() - time.getTime() );
document.getElementById('nextmin').innerHTML = left.getMinutes()
document.getElementById('nexthrs').innerHTML = left.getHours()
document.getElementById('nextsec').innerHTML = left.getSeconds()
}
else{
console.log("nope")
}
}
<div class="details">
<div class="next-prayer">
<span id="nexthrs">00</span>
<span>:</span>
<span id="nextmin">00</span>
<span>:</span>
<span id="nextsec">00</span>
</div>
</div>
but it keeps getting stuck on 00:00:00
4
Answers
You can get the timestamp of both, substract them and transform the result into a new Date format can’t you? Check the link to do it:
https://plainenglish.io/blog/how-to-convert-a-date-string-to-timestamp-in-javascript
hi see this code,
Just create
remainingTime
functions that calculate differences between two times and then returnhours
,minutes
, andseconds
values with a single object.The problem is that today’s times may already all be in the past. Your screenshot shows a local time of 19:24 which obviously is later than the two times you have in the
prayers
array. In that case you should look for the times for the next day.If in that case you want to have the time until the next prayer the day after, then add one more entry to your array, with a time that has 24 hours more than the first entry. For this to work you must first make sure the times are listed in chronological order (which is not the case in your example snippet)
Here is how you could do that (see comments in code):