Skip to main content

Prayer times, part 3: sunrise, Dhuhr and Maghrib

· 34 min read
Rizki Rakasiwi
Muslimtify maintainer

Part 2 ended with the sun at its highest over Jakarta at 11:38. That is the first prayer time in the series, even if we did not call it one. This part adds the second ingredient, your latitude, and with it the moment the sun crosses the horizon. By the end you will have three times that you can check against the sky and against the Kemenag timetable: sunrise, Dhuhr and Maghrib.

This is part 3 of a series

Part 1 covered the sources. Part 2 computed the sun's declination and the equation of time for any date. This part turns them into the first three prayer times. Part 4 points the same tool below the horizon for Fajr and Isha. The full series is under the prayer time calculation tag.

Dhuhr is the easy one​

From the hadith of Abdullah ibn Amr in part 1, Dhuhr begins "when the sun has declined", idha zalat ash-shams. The sun declines the instant it passes its highest point. We computed that instant in part 2 and called it solar noon:

noon = 12 + timezone - longitude / 15 - equation_of_time / 60

That is Dhuhr. There is nothing more to compute. For Jakarta on 17 October 2026 it is 11:38.1.

What the published timetable adds is precaution, not astronomy. Kemenag adds two minutes of ihtiyat and rounds up to the next whole minute, so 11:38.1 becomes 11:40.1 and then 11:41. The hadith of Uqbah in part 1 forbids praying while the sun stands at its peak, and two minutes is more than enough for the sun to have visibly moved on. Other authorities use a different margin or none, which is one of the differences part 6 will go through.

The sun's day as a clock​

To find sunrise and sunset we need a new idea, and it is the only new idea in this part.

The earth turns once every 24 hours, which is 15° per hour. Picture the sun's daily path as a clock face with noon at the top and midnight at the bottom. The sun's position on that face, measured in degrees from noon, is called its hour angle. At noon the hour angle is 0°. Three hours after noon it is 45°. At midnight it is 180°.

noon, H = 0°midnight, H = 180°daylight15° per hournightHsunsetsunriseJakarta, 17 October 2026: H = 91.9°, which is 6 h 7 min either side of noon
The sun's day as a clock face. The earth turns 15° an hour, so the hour angle H from noon to sunset, in degrees, is the length of the afternoon. Sunrise sits the same angle on the other side.

Every event in a prayer timetable is a question of the form: at what hour angle does the sun reach a certain height above or below the horizon? Answer that in degrees, divide by 15 to get hours, and add or subtract from noon.

The formula that answers it is the one piece of trigonometry in the whole series:

cos(H) = ( sin(altitude) - sin(latitude) * sin(declination) ) / ( cos(latitude) * cos(declination) )

Three inputs. The altitude is the height of the sun you are asking about, 0° for the horizon, negative below it. The latitude is yours. The declination is the sun's latitude, which part 2 gave us for any date. Out comes H, the hour angle, and H / 15 is the number of hours before or after noon.

You do not need to derive it, but you can see that it makes sense. Stand on the equator at an equinox, so latitude and declination are both 0. The formula collapses to cos(H) = sin(altitude). Ask for the horizon, altitude = 0, and you get cos(H) = 0, so H = 90°, which is six hours. Sunrise six hours before noon and sunset six hours after, a twelve-hour day. That is right.

What latitude does​

Now move away from the equator. The sun still goes around once a day, but its path no longer stands straight up from the horizon. It leans. At latitude 40° it crosses the horizon at a 50° slant. At latitude 60° it crosses at 30°. Near the poles it barely crosses at all.

Two things follow from the lean, and both matter for prayer times.

The first is the length of the day. When the sun's declination is on your side of the equator, the leaning path puts more than half of the sun's circle above your horizon, and days are long. When the declination is on the other side, days are short. In Jakarta, 6° south of the equator, the lean is tiny and days are always close to 12 hours. In Oslo they swing from 6 hours to 19.

The second is how long twilight lasts, and this is the one that will matter for Fajr and Isha in part 4. Fajr begins when the sun is about 18° below the horizon, and it ends at sunrise. Between those two moments the sun has to climb 18°. Near the equator it climbs almost straight up and takes about 70 minutes. In Oslo it climbs at a slant and takes well over two hours on the same date. The twilight band is the same 18° everywhere. The path through it is not.

−18°Jakarta, 6° Sthe sun climbs at 84° to the horizon−18° to sunrise: 70 min−18°Istanbul, 41° Nthe sun climbs at 49° to the horizon−18° to sunrise: 92 min−18°Oslo, 60° Nthe sun climbs at 30° to the horizon−18° to sunrise: 139 minSame date, 17 October 2026. The twilight band is the same 18° everywhere, but a shallower path takes longer to cross it.
Latitude sets the angle at which the sun crosses the horizon. Near the equator it climbs almost straight up and twilight is short. Far north it climbs at a slant, and the same 18° of twilight takes much longer.

Why sunrise is not at 0°​

If sunrise means "the first edge of the sun appears", the sun's center is not on the horizon at that moment. Two small things put it lower.

The sun is a disc, not a point, about 0.53° across. When its top edge touches the horizon, its center is still half a disc below it: 0.267°.

The atmosphere bends light. Near the horizon, light from the sun curves around the earth's edge a little on its way to your eye, and you see the sun about 0.567° higher than it really is. Astronomers call it refraction. It is the same effect that makes a straw in a glass of water look bent.

horizon, 0°where the sun really isits center is 0.833° below the horizonwhere you see itits top edge touches the horizon0.833°the air lifts it 0.567°
Why sunrise and sunset use −0.833° instead of 0°. Half the sun's width (0.267°) plus the lift from the atmosphere (0.567°) is where the real sun sits when you see its first or last edge.

Add the two and you get 0.833°. That is the number every prayer time library uses: sunrise and sunset are the moments the sun's center is 0.833° below the horizon. Maghrib is sunset. If you set the altitude to 0 instead, every sunset in the world would come out about three and a half minutes early. In Jakarta Maghrib would be announced while a sliver of the sun was still showing.

The 0.567° is an average. Refraction changes a little with temperature and air pressure, which is one of the reasons two careful calculators can differ by a few seconds at sunset. Nobody adjusts for it in a prayer timetable, and a minute of precaution covers it.

Getting the moment right​

Part 2 computed the sun's position once, at the start of the day in UTC, and used it for everything. That is fine for noon, but the declination changes by up to 0.4° a day, and sunset is three quarters of a day after our 0h UTC snapshot. An error of 0.3° in declination can move a sunset by a minute at mid latitudes.

The fix is simple and worth understanding, because every serious implementation does some version of it. Compute a first guess for the event using the declination at 0h UTC. Then recompute the sun's position at the moment of that guess and solve again. The second answer is accurate to seconds. In the code this is a loop that runs twice.

libmuslim goes one step further and brackets the exact moment with a binary search on the sun's altitude, which handles a few awkward days in the polar regions where the simple loop gets confused. Part 7 looks at those. For every city where most readers live, two rounds of the loop and the binary search agree to the second.

The code​

Two changes from part 2. sun_position now takes a Julian day instead of a calendar date, so we can ask for the sun's position at any moment of the day, not only at midnight. And there are three new functions: hour_angle, the formula above; sun_event, which finds when the sun reaches a given altitude before or after noon and refines the answer once; and hm, which prints hours as a clock time, rounding up when asked, because Kemenag does.

Pick your language. The choice is remembered from part 2.

import math

def julian_day(year, month, day):
if month <= 2:
year -= 1
month += 12
a = year // 100
b = 2 - a + a // 4
return math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + day + b - 1524.5

def sun_position(jd):
d = jd - 2451545.0 # days since J2000.0
g = math.radians((357.529 + 0.98560028 * d) % 360)
q = (280.459 + 0.98564736 * d) % 360
L = math.radians((q + 1.915 * math.sin(g) + 0.020 * math.sin(2 * g)) % 360)
e = math.radians(23.439 - 0.00000036 * d)
declination = math.degrees(math.asin(math.sin(e) * math.sin(L)))
right_ascension = math.degrees(math.atan2(math.cos(e) * math.sin(L), math.cos(L))) / 15
equation_of_time = (q / 15 - right_ascension + 12) % 24 - 12
return declination, equation_of_time * 60 # degrees, minutes

def hour_angle(latitude, declination, altitude):
"""Hours between solar noon and the moment the sun stands at `altitude` degrees."""
lat, dec, alt = (math.radians(v) for v in (latitude, declination, altitude))
cos_h = (math.sin(alt) - math.sin(lat) * math.sin(dec)) / (math.cos(lat) * math.cos(dec))
if cos_h < -1 or cos_h > 1:
return None # the sun never reaches that altitude today
return math.degrees(math.acos(cos_h)) / 15

SUNRISE_ALTITUDE = -0.833 # refraction 0.567 + half the sun's width 0.267

def solar_noon(jd, longitude, timezone):
_, eqt = sun_position(jd)
return 12 + timezone - longitude / 15 - eqt / 60 # local clock hours

def sun_event(jd, latitude, longitude, timezone, altitude, direction):
"""Local clock time when the sun reaches `altitude`, before noon (-1) or after (+1)."""
t = solar_noon(jd, longitude, timezone) # first guess, then refine at the event itself
for _ in range(2):
dec, _ = sun_position(jd + (t - timezone) / 24)
h = hour_angle(latitude, dec, altitude)
if h is None:
return None
t = solar_noon(jd + (t - timezone) / 24, longitude, timezone) + direction * h
return t

def hm(t, round_up=False):
if t is None:
return "--:--"
minutes = math.ceil(t * 60) if round_up else round(t * 60)
return f"{int(minutes // 60) % 24:02d}:{int(minutes % 60):02d}"

if __name__ == "__main__":
jd = julian_day(2026, 10, 17)
lat, lon, tz = -6.2088, 106.8456, 7 # Jakarta, WIB
noon = solar_noon(jd, lon, tz)
rise = sun_event(jd, lat, lon, tz, SUNRISE_ALTITUDE, -1)
sunset = sun_event(jd, lat, lon, tz, SUNRISE_ALTITUDE, +1)
print(f"sunrise {hm(rise)} (end of Fajr)")
print(f"dhuhr {hm(noon)} (solar noon)")
print(f"maghrib {hm(sunset)} (sunset)")
ihtiyat = 2 / 60
print(f"kemenag dhuhr {hm(noon + ihtiyat, round_up=True)}, maghrib {hm(sunset + ihtiyat, round_up=True)}")

Two lines deserve a comment.

jd + (t - timezone) / 24 is how we ask for the sun's position at a particular moment. jd is midnight UTC at the start of the date, t is a local clock time in hours, and subtracting the timezone turns it into UTC. Divide by 24 and it becomes a fraction of a day to add to the Julian day.

hour_angle returns nothing when cos(H) falls outside −1 to 1. That is not an error. It is the formula telling you that the sun never reaches that altitude today: no sunrise at all in the polar night, or no true darkness in a northern summer. Part 7 is about what a timetable should do then. For Jakarta it never happens.

Check your work​

Run the program. For Jakarta on 17 October 2026 you should see:

sunrise 05:31 (end of Fajr)
dhuhr 11:38 (solar noon)
maghrib 17:45 (sunset)
kemenag dhuhr 11:41, maghrib 17:48

The last line is what the Kemenag timetable for Jakarta prints for that day, and what Muslimtify shows with the default method. The three lines above it are the astronomy before precaution and rounding.

Now check against something independent. The NOAA solar calculator from part 2 gives sunrise and sunset for any place and date. For Jakarta on 17 October it should agree with the first and third lines to the minute.

Then check against the sky. Sunset is the easiest prayer time to see with your own eyes. On 17 October, if the western horizon in Jakarta is clear, the last edge of the sun drops out of sight at 17:45 and a half. The adhan for Maghrib from the nearest mosque will follow about two and a half minutes later, which is the ihtiyat and the rounding. If it comes earlier than the sun, something is wrong with the mosque's table, not with the sun.

Finally, try your own city. Change the three numbers on the lat, lon, tz line and run it again. If you live north of about 50°, try 21 June as well as 17 October and watch the day length change.

What to carry into part 4​

One new formula, the hour angle, which turns "when does the sun reach this height" into "this many hours from noon". One new constant, 0.833°, which is where the sun's center sits when its edge touches the horizon. One new habit, refining the answer at the moment of the event. With those three, Dhuhr, sunrise and Maghrib are done, and they match the production library to the second.

Part 4 asks the same question with a different altitude. Instead of 0.833° below the horizon, Fajr asks for 18° or 20°, and Isha asks for 17° or 18°. The formula does not change at all. What changes is where those numbers come from, why they differ from country to country, and what the sky looks like when the sun is that far below the horizon. That is the post where the hadith about the white thread meets the code.