You are on page 1of 5

prayTime.js prayTime.

js
//--------------------- Copyright Block ---------------------- this.MWL = 3; // Muslim World League (MWL)
/* this.Makkah = 4; // Umm al-Qura, Makkah
this.Egypt = 5; // Egyptian General Authority of Survey
PrayTime: Prayer Times Calculator (ver 1.1) this.Custom = 6; // Custom Setting
Copyright (C) 2007, Hamid Zarrabi-Zadeh
// Juristic Methods
This program is free software; you can redistribute it and/or this.Shafii = 0; // Shafii (standard)
modify it under the terms of the GNU General Public License this.Hanafi = 1; // Hanafi
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. // Adjusting Methods for Higher Latitudes
this.None = 0; // No adjustment
This program is distributed in the hope that it will be useful, this.MidNight = 1; // middle of night
but WITHOUT ANY WARRANTY; without even the implied warranty of this.OneSeventh = 2; // 1/7th of night
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the this.AngleBased = 3; // angle/60th of night
GNU General Public License for more details.
You can get a copy of the GNU General Public License from // Time Formats
http://www.gnu.org/copyleft/gpl.html this.Time24 = 0; // 24-hour format
this.Time12 = 1; // 12-hour format
this.Time12NS = 2; // 12-hour format with no suffix
this.Float = 3; // floating point number
//--------------------- Help and Manual ----------------------
// Time Names
User's Manual: this.timeNames = new Array(
http://tanzil.info/praytime/doc/manual 'Fajr',
'Sunrise',
Calculating Formulas: 'Dhuhr',
http://tanzil.info/praytime/doc/calculation 'Asr',
'Sunset',
*/ 'Maghrib',
'Isha'
);
//--------------------- PrayTime Class -----------------------
this.InvalidTime = '-----'; // The string used for invalid times
function PrayTime()
{
//---------------------- Global Variables --------------------
//--------------------- User Interface -----------------------
/*
this.calcMethod = 0; // caculation method
getPrayerTimes (date, latitude, longitude, timeZone) this.asrJuristic = 0; // Juristic method for Asr
getDatePrayerTimes (year, month, day, latitude, longitude, timeZone) this.dhuhrMinutes = 0; // minutes after mid-day for Dhuhr
this.adjustHighLats = 1; // adjusting method for higher latitudes
setCalcMethod (methodID)
setAsrMethod (methodID) this.timeFormat = 0; // time format
setFajrAngle (angle) var lat; // latitude
setMaghribAngle (angle) var lng; // longitude
setIshaAngle (angle) var timeZone; // time-zone
setDhuhrMinutes (minutes) // minutes after mid-day var JDate; // Julian date
setMaghribMinutes (minutes) // minutes after sunset
setIshaMinutes (minutes) // minutes after maghrib
//--------------------- Technical Settings --------------------
setHighLatsMethod (methodID) // adjust method for higher latitudes
setTimeFormat (timeFormat) this.numIterations = 1; // number of iterations needed to
floatToTime24 (time) compute times
floatToTime12 (time)
floatToTime12NS (time)
*/
//------------------------ Constants -------------------------- //------------------- Calc Method Parameters --------------------
// Calculation Methods this.methodParams = new Array();
this.Jafari = 0; // Ithna Ashari
this.Karachi = 1; // University of Islamic Sciences, Karachi /* this.methodParams[methodNum] = new Array(fa, ms, mv, is, iv);
this.ISNA = 2; // Islamic Society of North America (ISNA)
Page 1 Page 2
prayTime.js prayTime.js
fa : fajr angle this.setCustomParams(new Array(null, 0, angle, null, null));
ms : maghrib selector (0 = angle; 1 = minutes after }
sunset)
mv : maghrib parameter value (in angle or minutes) // set the angle for calculating Isha
is : isha selector (0 = angle; 1 = minutes after PrayTime.prototype.setIshaAngle = function(angle)
maghrib) {
iv : isha parameter value (in angle or minutes) this.setCustomParams(new Array(null, null, null, 0, angle));
*/ }
this.methodParams[this.Jafari] = new Array(16, 0, 4, 0, 14); // set the minutes after mid-day for calculating Dhuhr
this.methodParams[this.Karachi] = new Array(18, 1, 0, 0, 18); PrayTime.prototype.setDhuhrMinutes = function(minutes)
this.methodParams[this.ISNA] = new Array(15, 1, 0, 0, 15); {
this.methodParams[this.MWL] = new Array(18, 1, 0, 0, 17); this.dhuhrMinutes = minutes;
}
this.methodParams[this.Makkah] = new Array(19, 1, 0, 1, 90);
this.methodParams[this.Egypt] = new Array(19.5, 1, 0, 0, 17.5); // set the minutes after Sunset for calculating Maghrib
PrayTime.prototype.setMaghribMinutes = function(minutes)
this.methodParams[this.Custom] = new Array(18, 1, 0, 0, 17); {
this.setCustomParams(new Array(null, 1, minutes, null, null));
} }
// set the minutes after Maghrib for calculating Isha
//-------------------- Interface Functions -------------------- PrayTime.prototype.setIshaMinutes = function(minutes)
{
this.setCustomParams(new Array(null, null, null, 1, minutes));
// return prayer times for a given date }
PrayTime.prototype.getDatePrayerTimes = function(year, month, day, latitude,
longitude, timeZone) // set custom values for calculation parameters
{ PrayTime.prototype.setCustomParams = function(params)
this.lat = latitude; {
this.lng = longitude; for (var i=0; i<5; i++)
this.timeZone = this.effectiveTimeZone(year, month, day, timeZone); {
this.JDate = this.julianDate(year, month, day)- longitude/ (15* 24); if (params[i] == null)
return this.computeDayTimes(); this.methodParams[this.Custom][i] =
} this.methodParams[this.calcMethod][i];
else
// return prayer times for a given date this.methodParams[this.Custom][i] = params[i];
PrayTime.prototype.getPrayerTimes = function(date, latitude, longitude, }
timeZone) this.calcMethod = this.Custom;
{ }
return this.getDatePrayerTimes(date.getFullYear(), date.getMonth()+ 1,
date.getDate(), // set adjusting method for higher latitudes
latitude, longitude, timeZone); PrayTime.prototype.setHighLatsMethod = function(methodID)
} {
this.adjustHighLats = methodID;
// set the calculation method }
PrayTime.prototype.setCalcMethod = function(methodID)
{ // set the time format
this.calcMethod = methodID; PrayTime.prototype.setTimeFormat = function(timeFormat)
} {
this.timeFormat = timeFormat;
// set the juristic method for Asr }
PrayTime.prototype.setAsrMethod = function(methodID)
{ // convert float hours to 24h format
if (methodID < 0 || methodID > 1) PrayTime.prototype.floatToTime24 = function(time)
return; {
this.asrJuristic = methodID; if (isNaN(time))
} return this.InvalidTime;
time = this.fixhour(time+ 0.5/ 60); // add 0.5 minutes to round
// set the angle for calculating Fajr var hours = Math.floor(time);
PrayTime.prototype.setFajrAngle = function(angle) var minutes = Math.floor((time- hours)* 60);
{ return this.twoDigitsFormat(hours)+':'+ this.twoDigitsFormat(minutes);
this.setCustomParams(new Array(angle, null, null, null, null)); }
}
// convert float hours to 12h format
// set the angle for calculating Maghrib PrayTime.prototype.floatToTime12 = function(time, noSuffix)
PrayTime.prototype.setMaghribAngle = function(angle) {
{ if (isNaN(time))
Page 3 Page 4
prayTime.js prayTime.js
return this.InvalidTime; var Z = this.computeMidDay(t);
time = this.fixhour(time+ 0.5/ 60); // add 0.5 minutes to round var V = 1/15* this.darccos((-this.dsin(G)- this.dsin(D)*
var hours = Math.floor(time); this.dsin(this.lat)) /
var minutes = Math.floor((time- hours)* 60); (this.dcos(D)* this.dcos(this.lat)));
var suffix = hours >= 12 ? ' pm' : ' am'; return Z+ (G>90 ? -V : V);
hours = (hours+ 12 -1)% 12+ 1; }
return hours+':'+ this.twoDigitsFormat(minutes)+ (noSuffix ? '' :
suffix); // compute the time of Asr
} PrayTime.prototype.computeAsr = function(step, t) // Shafii: step=1, Hanafi:
step=2
// convert float hours to 12h format with no suffix {
PrayTime.prototype.floatToTime12NS = function(time) var D = this.sunDeclination(this.JDate+ t);
{ var G = -this.darccot(step+ this.dtan(Math.abs(this.lat-D)));
return this.floatToTime12(time, true); return this.computeTime(G, t);
} }
//---------------------- Compute Prayer Times -----------------------
//---------------------- Calculation Functions -----------------------
// References: // compute prayer times at given julian date
// http://www.ummah.net/astronomy/saltime PrayTime.prototype.computeTimes = function(times)
// http://aa.usno.navy.mil/faq/docs/SunApprox.html {
var t = this.dayPortion(times);
// compute declination angle of sun and equation of time var Fajr = this.computeTime(180-
PrayTime.prototype.sunPosition = function(jd) this.methodParams[this.calcMethod][0], t[0]);
{ var Sunrise = this.computeTime(180- 0.833, t[1]);
var D = jd - 2451545.0; var Dhuhr = this.computeMidDay(t[2]);
var g = this.fixangle(357.529 + 0.98560028* D); var Asr = this.computeAsr(1+ this.asrJuristic, t[3]);
var q = this.fixangle(280.459 + 0.98564736* D); var Sunset = this.computeTime(0.833, t[4]);;
var L = this.fixangle(q + 1.915* this.dsin(g) + 0.020* this.dsin(2*g)); var Maghrib = this.computeTime(this.methodParams[this.calcMethod][2],
t[5]);
var R = 1.00014 - 0.01671* this.dcos(g) - 0.00014* this.dcos(2*g); var Isha = this.computeTime(this.methodParams[this.calcMethod][4],
var e = 23.439 - 0.00000036* D; t[6]);
var d = this.darcsin(this.dsin(e)* this.dsin(L)); return new Array(Fajr, Sunrise, Dhuhr, Asr, Sunset, Maghrib, Isha);
var RA = this.darctan2(this.dcos(e)* this.dsin(L), this.dcos(L))/ 15; }
RA = this.fixhour(RA);
var EqT = q/15 - RA;
// compute prayer times at given julian date
return new Array(d, EqT); PrayTime.prototype.computeDayTimes = function()
} {
var times = new Array(5, 6, 12, 13, 18, 18, 18); //default times
// compute equation of time
PrayTime.prototype.equationOfTime = function(jd) for (var i=1; i<=this.numIterations; i++)
{ times = this.computeTimes(times);
return this.sunPosition(jd)[1];
} times = this.adjustTimes(times);
return this.adjustTimesFormat(times);
// compute declination angle of sun }
PrayTime.prototype.sunDeclination = function(jd)
{
return this.sunPosition(jd)[0]; // adjust times in a prayer time array
} PrayTime.prototype.adjustTimes = function(times)
{
// compute mid-day (Dhuhr, Zawal) time for (var i=0; i<7; i++)
PrayTime.prototype.computeMidDay = function(t) times[i] += this.timeZone- this.lng/ 15;
{ times[2] += this.dhuhrMinutes/ 60; //Dhuhr
var T = this.equationOfTime(this.JDate+ t); if (this.methodParams[this.calcMethod][1] == 1) // Maghrib
var Z = this.fixhour(12- T); times[5] = times[4]+ this.methodParams[this.calcMethod][2]/ 60;
return Z; if (this.methodParams[this.calcMethod][3] == 1) // Isha
} times[6] = times[5]+ this.methodParams[this.calcMethod][4]/ 60;
// compute time for a given angle G if (this.adjustHighLats != this.None)
PrayTime.prototype.computeTime = function(G, t) times = this.adjustHighLatTimes(times);
{ return times;
var D = this.sunDeclination(this.JDate+ t); }
Page 5 Page 6
prayTime.js prayTime.js
//---------------------- Misc Functions -----------------------
// convert times array to given time format
PrayTime.prototype.adjustTimesFormat = function(times)
{ // compute the difference between two times
if (this.timeFormat == this.Float) PrayTime.prototype.timeDiff = function(time1, time2)
return times; {
for (var i=0; i<7; i++) return this.fixhour(time2- time1);
if (this.timeFormat == this.Time12) }
times[i] = this.floatToTime12(times[i]);
else if (this.timeFormat == this.Time12NS)
times[i] = this.floatToTime12(times[i], true); // add a leading 0 if necessary
else PrayTime.prototype.twoDigitsFormat = function(num)
times[i] = this.floatToTime24(times[i]); {
return times; return (num <10) ? '0'+ num : num;
} }
// adjust Fajr, Isha and Maghrib for locations in higher latitudes
PrayTime.prototype.adjustHighLatTimes = function(times) //---------------------- Julian Date Functions -----------------------
{
var nightTime = this.timeDiff(times[4], times[1]); // sunset to sunrise
// calculate julian date from a calendar date
// Adjust Fajr PrayTime.prototype.julianDate = function(year, month, day)
var FajrDiff = this.nightPortion(this.methodParams[this.calcMethod][0])* {
nightTime; if (month <= 2)
if (isNaN(times[0]) || this.timeDiff(times[0], times[1]) > FajrDiff) {
times[0] = times[1]- FajrDiff; year -= 1;
month += 12;
// Adjust Isha }
var IshaAngle = (this.methodParams[this.calcMethod][3] == 0) ? var A = Math.floor(year/ 100);
this.methodParams[this.calcMethod][4] : 18; var B = 2- A+ Math.floor(A/ 4);
var IshaDiff = this.nightPortion(IshaAngle)* nightTime;
if (isNaN(times[6]) || this.timeDiff(times[4], times[6]) > IshaDiff) var JD = Math.floor(365.25* (year+ 4716))+ Math.floor(30.6001* (month+
times[6] = times[4]+ IshaDiff; 1))+ day+ B- 1524.5;
return JD;
// Adjust Maghrib }
var MaghribAngle = (this.methodParams[this.calcMethod][1] == 0) ?
this.methodParams[this.calcMethod][2] : 4;
var MaghribDiff = this.nightPortion(MaghribAngle)* nightTime; // convert a calendar date to julian date (second method)
if (isNaN(times[5]) || this.timeDiff(times[4], times[5]) > MaghribDiff) PrayTime.prototype.calcJD = function(year, month, day)
times[5] = times[4]+ MaghribDiff; {
var J1970 = 2440588.0;
return times; var date = new Date(year, month- 1, day);
} var ms = date.getTime(); // # of milliseconds since midnight Jan 1,
1970
var days = Math.floor(ms/ (1000 * 60 * 60* 24));
// the night portion used for adjusting times in higher latitudes return J1970+ days- 0.5;
PrayTime.prototype.nightPortion = function(angle) }
{
if (this.adjustHighLats == this.AngleBased)
return 1/60* angle; //---------------------- Time-Zone Functions -----------------------
if (this.adjustHighLats == this.MidNight)
return 1/2;
if (this.adjustHighLats == this.OneSeventh) // compute local time-zone for a specific date
return 1/7; PrayTime.prototype.getTimeZone = function(date)
} {
var localDate = new Date(date.getFullYear(), date.getMonth(),
date.getDate(), 0, 0, 0, 0);
// convert hours to day portions var GMTString = localDate.toGMTString();
PrayTime.prototype.dayPortion = function(times) var GMTDate = new Date(GMTString.substring(0, GMTString.lastIndexOf('
{ ')- 1));
for (var i=0; i<7; i++) var hoursDiff = (localDate- GMTDate) / (1000* 60* 60);
times[i] /= 24; return hoursDiff;
return times; }
}
// compute base time-zone of the system
Page 7 Page 8
prayTime.js prayTime.js
PrayTime.prototype.getBaseTimeZone = function() PrayTime.prototype.darccot = function(x)
{ {
return this.getTimeZone(new Date(2000, 0, 15)) return this.rtd(Math.atan(1/x));
} }
// degree to radian
// detect daylight saving in a given date PrayTime.prototype.dtr = function(d)
PrayTime.prototype.detectDaylightSaving = function(date) {
{ return (d * Math.PI) / 180.0;
return this.getTimeZone(date) != this.getBaseTimeZone(); }
}
// radian to degree
PrayTime.prototype.rtd = function(r)
// return effective timezone for a given date {
PrayTime.prototype.effectiveTimeZone = function(year, month, day, timeZone) return (r * 180.0) / Math.PI;
{ }
if (timeZone == null || typeof(timeZone) == 'undefined' || timeZone ==
'auto') // range reduce angle in degrees.
timeZone = this.getTimeZone(new Date(year, month- 1, day)); PrayTime.prototype.fixangle = function(a)
return 1* timeZone; {
} a = a - 360.0 * (Math.floor(a / 360.0));
a = a < 0 ? a + 360.0 : a;
return a;
//---------------------- Trigonometric Functions ----------------------- }
// degree sin // range reduce hours to 0..23
PrayTime.prototype.dsin = function(d) PrayTime.prototype.fixhour = function(a)
{ {
return Math.sin(this.dtr(d)); a = a - 24.0 * (Math.floor(a / 24.0));
} a = a < 0 ? a + 24.0 : a;
return a;
// degree cos }
PrayTime.prototype.dcos = function(d)
{
return Math.cos(this.dtr(d)); //---------------------- prayTime Object -----------------------
}
var prayTime = new PrayTime();
// degree tan
PrayTime.prototype.dtan = function(d)
{
return Math.tan(this.dtr(d));
}
// degree arcsin
PrayTime.prototype.darcsin = function(x)
{
return this.rtd(Math.asin(x));
}
// degree arccos
PrayTime.prototype.darccos = function(x)
{
return this.rtd(Math.acos(x));
}
// degree arctan
PrayTime.prototype.darctan = function(x)
{
return this.rtd(Math.atan(x));
}
// degree arctan2
PrayTime.prototype.darctan2 = function(y, x)
{
return this.rtd(Math.atan2(y, x));
}
// degree arccot
Page 9 Page 10

You might also like