Blog / Development
Build an iftar and suhoor countdown widget in JavaScript
An iftar countdown is a small, genuinely useful Ramadan feature. The tricky part is not the timer; it is getting the right times for each user.
By Abdullah NomanDecember 3, 20263 min read

Short answer: compute sunset (Maghrib, for iftar) and dawn (Fajr, for the end of suhoor) for the user's coordinates with a prayer-time library such as the open-source Adhan library, let the user choose the calculation method, and run a countdown from Date.now() to the next of those two times.
1. Get the times
Do not hard-code a timetable. Times change every day and by location. A calculation library takes coordinates, a date and a method:
import { Coordinates, CalculationMethod, PrayerTimes } from 'adhan';
const coords = new Coordinates(23.8103, 90.4125); // Dhaka
const params = CalculationMethod.Karachi(); // pick per region or let the user choose
const times = new PrayerTimes(coords, new Date(), params);
const iftar = times.maghrib; // sunset
const suhoorEnds = times.fajr; // dawn
Many communities follow their local mosque's timetable, which can differ by a few minutes. Say so, and let users adjust by a few minutes if needed.
2. Choose the method carefully
Dawn is calculated using different sun angles by different authorities, and the difference can be several minutes. Default by region, show the method name, and let users change it in settings.
3. The countdown
function nextEvent(now, today, tomorrow) {
if (now < today.fajr) return { label: 'Suhoor ends', at: today.fajr };
if (now < today.maghrib) return { label: 'Iftar', at: today.maghrib };
return { label: 'Suhoor ends', at: tomorrow.fajr };
}
setInterval(() => {
const now = new Date();
const e = nextEvent(now, todayTimes, tomorrowTimes);
const s = Math.max(0, Math.floor((e.at - now) / 1000));
el.textContent = `${e.label} in ${Math.floor(s / 3600)}h ${Math.floor(s % 3600 / 60)}m`;
}, 1000);
Compute from the current clock every tick so background tabs do not drift.
4. Time zones
The library returns real Date objects; format them in the location's time zone with Intl.DateTimeFormat and its timeZone option, especially if users can look up another city.
5. Accessibility and tone
- Show minutes, not seconds, to reduce noise; update screen readers once a minute at most.
- Label the source: "Times calculated for Dhaka, Karachi method."
- Add a small illustration, not an animation.

Questions
How do I calculate iftar time in JavaScript?
Use a prayer-time library such as Adhan with the user’s coordinates and a calculation method; iftar is at Maghrib (sunset).
Why are suhoor times different between apps?
Dawn (Fajr) is calculated with different sun angles by different authorities. Show the method and let users choose.
Should I hard-code a Ramadan timetable?
No. Times change daily and by location. Calculate them, or load your local mosque’s timetable.
How do I avoid countdown drift?
Recompute the remaining time from the current clock on every tick.


