<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: عبدالله الرياض</title>
    <description>The latest articles on Forem by عبدالله الرياض (@abdulla1).</description>
    <link>https://forem.com/abdulla1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3724538%2Fe7c5540d-2a75-496b-aa69-d4771c6e392a.webp</url>
      <title>Forem: عبدالله الرياض</title>
      <link>https://forem.com/abdulla1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abdulla1"/>
    <language>en</language>
    <item>
      <title>How to Calculate Accurate Prayer Times Using JavaScript (Riyadh Case Study)</title>
      <dc:creator>عبدالله الرياض</dc:creator>
      <pubDate>Wed, 21 Jan 2026 19:07:45 +0000</pubDate>
      <link>https://forem.com/abdulla1/how-to-calculate-accurate-prayer-times-using-javascript-riyadh-case-study-3258</link>
      <guid>https://forem.com/abdulla1/how-to-calculate-accurate-prayer-times-using-javascript-riyadh-case-study-3258</guid>
      <description>&lt;p&gt;Prayer time calculation is one of those problems that looks simple until you actually try to build it. Once you dig in, you’re dealing with astronomy, time zones, calculation standards, and edge cases that can easily break accuracy.&lt;/p&gt;

&lt;p&gt;In this article, I’ll explain &lt;strong&gt;how prayer times are calculated&lt;/strong&gt;, show &lt;strong&gt;JavaScript-based logic&lt;/strong&gt;, and share how I applied it in a real website focused on &lt;strong&gt;Riyadh prayer times&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Prayer Times Aren’t Static
&lt;/h2&gt;

&lt;p&gt;Prayer times are based on the &lt;strong&gt;sun’s position&lt;/strong&gt;, not the clock. That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Times change &lt;strong&gt;every day&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Location (latitude &amp;amp; longitude) matters&lt;/li&gt;
&lt;li&gt;Different regions follow &lt;strong&gt;different calculation methods&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Small errors = wrong prayer times&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For cities like Riyadh, users expect &lt;strong&gt;high accuracy&lt;/strong&gt;, not estimates.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Data Required
&lt;/h2&gt;

&lt;p&gt;To calculate prayer times, you need:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Geographic Coordinates
&lt;/h3&gt;

&lt;p&gt;For Riyadh:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Latitude: &lt;code&gt;24.7136&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Longitude: &lt;code&gt;46.6753&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These values are used to calculate solar angles.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Calculation Method
&lt;/h3&gt;

&lt;p&gt;There are multiple standards:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Muslim World League&lt;/li&gt;
&lt;li&gt;ISNA&lt;/li&gt;
&lt;li&gt;Egyptian&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Umm Al-Qura (Saudi Arabia)&lt;/strong&gt; ✅&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Riyadh, &lt;strong&gt;Umm Al-Qura&lt;/strong&gt; is the correct and widely accepted choice.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Time Zone
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Arabia Standard Time (&lt;strong&gt;UTC +3&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;No daylight saving time (simplifies things)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  JavaScript Approach: Library vs Manual Math
&lt;/h2&gt;

&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; write all astronomical math yourself, but in practice, most developers use a &lt;strong&gt;well-tested library&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A popular choice is &lt;strong&gt;Adhan.js&lt;/strong&gt;, which implements official calculation methods correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example: Calculating Prayer Times in JavaScript
&lt;/h2&gt;

&lt;p&gt;Here’s a simplified example using JavaScript logic similar to what libraries do internally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrayerTimes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Coordinates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;CalculationMethod&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adhan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Coordinates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;24.7136&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;46.6753&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Umm Al-Qura method (Saudi Arabia)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;CalculationMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UmmAlQura&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrayerTimes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coordinates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;fajr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fajr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dhuhr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dhuhr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;asr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;asr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maghrib&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maghrib&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;isha&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isha&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This handles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solar angles&lt;/li&gt;
&lt;li&gt;Location&lt;/li&gt;
&lt;li&gt;Correct calculation method&lt;/li&gt;
&lt;li&gt;Daily variations&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Formatting Times for Users
&lt;/h2&gt;

&lt;p&gt;Raw &lt;code&gt;Date&lt;/code&gt; objects aren’t user-friendly. You’ll usually want formatted output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formatTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-US&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2-digit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;minute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2-digit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hour12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;formatTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prayerTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fajr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// e.g. 05:12 AM&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Real-World Implementation: Riyadh-Focused Website
&lt;/h2&gt;

&lt;p&gt;I applied this logic in a production website dedicated entirely to &lt;strong&gt;&lt;a href="https://prayertimesinriyadh.com/" rel="noopener noreferrer"&gt;this WebApplication&lt;/a&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Key decisions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;City-specific focus (no geolocation guessing)&lt;/li&gt;
&lt;li&gt;Umm Al-Qura calculation method&lt;/li&gt;
&lt;li&gt;Daily recalculation for accuracy&lt;/li&gt;
&lt;li&gt;Cached results per day (performance)&lt;/li&gt;
&lt;li&gt;Clean, distraction-free UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By narrowing the scope to &lt;strong&gt;one city&lt;/strong&gt;, accuracy and speed both improved.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes to Avoid
&lt;/h2&gt;

&lt;p&gt;If you’re building something similar, watch out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Using the wrong calculation method&lt;/li&gt;
&lt;li&gt;❌ Hardcoding prayer times&lt;/li&gt;
&lt;li&gt;❌ Incorrect time zone offsets&lt;/li&gt;
&lt;li&gt;❌ Not updating data daily&lt;/li&gt;
&lt;li&gt;❌ Over-caching results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prayer times are &lt;strong&gt;data-sensitive&lt;/strong&gt; — small mistakes add up fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  When APIs Make Sense (and When They Don’t)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;APIs are great if:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to visit many cities quickly&lt;/li&gt;
&lt;li&gt;You don’t want to manage calculations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Local calculation is better if:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want full control&lt;/li&gt;
&lt;li&gt;You need reliability&lt;/li&gt;
&lt;li&gt;Accuracy is critical&lt;/li&gt;
&lt;li&gt;You want offline support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For my use case, &lt;strong&gt;local calculation won&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Prayer time calculation is a great example of how &lt;strong&gt;domain knowledge matters as much as code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you’re building applications for real users — especially religious or time-sensitive ones — accuracy isn’t a “nice to have”, it’s essential.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>islamictech</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
