Quick answer: Prayer times are calculated from the position of the sun. Dhuhr is just after solar noon, Maghrib is at sunset, Asr is when an object's shadow reaches its length (or twice its length in the Hanafi method) plus the noon shadow, and Fajr and Isha are when the sun is a certain angle below the horizon. Different organisations use different angles – for example the Umm al-Qura method used in Saudi Arabia sets Fajr at 18.5° and Isha at 90 minutes after Maghrib (120 minutes in Ramadan).

Every Muslim app, smart speaker and mosque display needs accurate prayer times. As a developer, you do not need to be an astronomer – but you do need to understand the rules, so that your app gives correct times and lets users choose their method. This article explains the concepts and gives working JavaScript code.

The Islamic definitions

Prayer

Begins when...

Fajr

True dawn appears – sun at a set angle below the eastern horizon

Sunrise

Upper edge of the sun appears (end of Fajr time)

Dhuhr

Sun passes its highest point (zawal) and begins to decline

Asr

Shadow length = object length × factor + noon shadow (factor 1 standard, 2 Hanafi)

Maghrib

Sun has fully set

Isha

Twilight disappears – sun at a set angle below horizon, or fixed minutes after Maghrib

These definitions come from the hadith of Jibril leading the Prophet ﷺ in prayer at the beginning and end of each time (Abu Dawud 393, Tirmidhi 149) and the hadith of Abdullah ibn Amr in Sahih Muslim (612).

Common calculation methods

Method

Fajr angle

Isha

Used in

Umm al-Qura (Makkah)

18.5°

90 min after Maghrib (120 in Ramadan)

Saudi Arabia

Muslim World League

18°

17°

Europe, Far East, many apps

ISNA

15°

15°

North America

Egyptian General Authority

19.5°

17.5°

Africa, Syria, Lebanon

University of Islamic Sciences, Karachi

18°

18°

Pakistan, India, Bangladesh

Step 1: Sun position for a date

We need two values for the day: the sun's declination (how far north/south it is) and the equation of time (difference between clock noon and solar noon).

const rad = d => d * Math.PI / 180, deg = r => r * 180 / Math.PI;
const fix = (a, b) => ((a % b) + b) % b;

function julian(y, m, d) {
  if (m <= 2) { y -= 1; m += 12; }
  const A = Math.floor(y / 100), B = 2 - A + Math.floor(A / 4);
  return Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + d + B - 1524.5;
}

function sunPosition(jd) {
  const D = jd - 2451545.0;
  const g = fix(357.529 + 0.98560028 * D, 360);
  const q = fix(280.459 + 0.98564736 * D, 360);
  const L = fix(q + 1.915 * Math.sin(rad(g)) + 0.020 * Math.sin(rad(2 * g)), 360);
  const e = 23.439 - 0.00000036 * D;
  const RA = deg(Math.atan2(Math.cos(rad(e)) * Math.sin(rad(L)), Math.cos(rad(L)))) / 15;
  const eqt = q / 15 - fix(RA, 24);
  const decl = deg(Math.asin(Math.sin(rad(e)) * Math.sin(rad(L))));
  return { decl, eqt };
}

Step 2: Solar noon and hour angles

function hourAngle(angle, lat, decl) {
  // time (hours) between noon and when the sun is 'angle' degrees below horizon
  const v = (-Math.sin(rad(angle)) - Math.sin(rad(decl)) * Math.sin(rad(lat))) /
            (Math.cos(rad(decl)) * Math.cos(rad(lat)));
  return deg(Math.acos(v)) / 15;
}

function asrTime(factor, lat, decl) {
  const alt = deg(Math.atan(1 / (factor + Math.tan(rad(Math.abs(lat - decl))))));
  const v = (Math.sin(rad(alt)) - Math.sin(rad(decl)) * Math.sin(rad(lat))) /
            (Math.cos(rad(decl)) * Math.cos(rad(lat)));
  return deg(Math.acos(v)) / 15;
}

Step 3: Put it together

function prayerTimes(date, lat, lng, tz, opt = { fajr: 18.5, ishaMinutes: 90, asr: 1 }) {
  const jd = julian(date.getFullYear(), date.getMonth() + 1, date.getDate()) - lng / 360;
  const { decl, eqt } = sunPosition(jd + 0.5);
  const noon = 12 + tz - lng / 15 - eqt;
  const sunrise = noon - hourAngle(0.833, lat, decl);
  const sunset  = noon + hourAngle(0.833, lat, decl);
  const t = {
    fajr: noon - hourAngle(opt.fajr, lat, decl),
    sunrise,
    dhuhr: noon + 1 / 60,          // small safety margin after zawal
    asr: noon + asrTime(opt.asr, lat, decl),
    maghrib: sunset,
    isha: opt.ishaMinutes ? sunset + opt.ishaMinutes / 60
                          : noon + hourAngle(opt.isha, lat, decl)
  };
  const fmt = h => { h = fix(h, 24); const m = Math.round(h * 60);
    return String(Math.floor(m / 60) % 24).padStart(2, '0') + ':' + String(m % 60).padStart(2, '0'); };
  return Object.fromEntries(Object.entries(t).map(([k, v]) => [k, fmt(v)]));
}

// Riyadh: lat 24.7136, lng 46.6753, UTC+3
console.log(prayerTimes(new Date(), 24.7136, 46.6753, 3));

Important details for production apps

  • Let users choose the method and Asr juristic setting (Standard/Hanafi).
  • Ramadan rule for Umm al-Qura: Isha is 120 minutes after Maghrib.
  • High latitudes (UK, Scandinavia): in summer the sun may not reach 18° below the horizon. Use an adjustment rule such as "middle of the night", "one-seventh of the night" or "angle-based".
  • Elevation affects sunrise and sunset slightly in mountains.
  • Daylight saving time – use the IANA time zone (for example Europe/London) rather than a fixed offset.
  • Manual offsets – mosques often add a few minutes; allow per-prayer adjustments.
  • Validate against official timetables (for Saudi Arabia, the Umm al-Qura calendar) in several cities and seasons.

Tip: For production, consider a well-tested open-source library such as Adhan (available for JavaScript, Swift, Kotlin, Dart and more) instead of maintaining your own astronomy code. Use the code above to understand and debug results.

Summary

  • Prayer times depend on sun declination, equation of time, location and method.
  • Fajr/Isha use angles (or minutes); Asr uses shadow factor 1 or 2.
  • Umm al-Qura: Fajr 18.5°, Isha 90 min (120 in Ramadan).
  • Handle high latitudes, time zones and user offsets in real apps.