-
Notifications
You must be signed in to change notification settings - Fork 2
/
islamicdate.js
131 lines (106 loc) · 2.9 KB
/
islamicdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Islamic Date Calculator
*
* Using a modified version of the "Kuwaiti Algorithm" or Tabular Islamic Calendar to calculate and convert
* Gregorian dates to Hijri dates.
*
* Main source : http://www.al-habib.info/islamic-calendar/hijricalendartext.htm
*
* To use: IC.getIslamicDate([date]);
* [date] : date to convert - leave null or unspecified to use the current date
*
* for todays date: IC.getIslamicDate();
* for an arbitrary date: IC.getIslamicDate(new Date("2012/12/25"));
*
*
* Suitable for browser and Node.js
*
* @anami
*/
(function(exports){
// constants
var iyear = 10631./30.,
epochastro = 1948084,
epochcivil = 1948085,
shift1 = 8.01/60.,
DAYNAMES = new Array("Ahad","Ithnain","Thulatha","Arbi'aa","Khamees","Jumuah","Sabt"),
MONTHNAMES = new Array("Muharram","Safar","Rabi'ul Awwal","Rabi'ul Akhir",
"Jumadal Ula","Jumadal Ukhra","Rajab","Sha'ban",
"Ramadan","Shawwal","Dhul Qa'adah","Dhul Hijjah");
function gmod(n,m){
return ((n%m)+m)%m;
}
function kuwaiticalendar(date){
var today = date || new Date();
var day = today.getDate(),
month = today.getMonth(),
year = today.getFullYear(),
m = month+1,
y = year;
if(m<3) {
y -= 1;
m += 12;
}
var a = Math.floor(y/100.),
b = 2-a+Math.floor(a/4.);
if(y<1583) b = 0;
if(y==1582) {
if(m>10) b = -10;
if(m==10) {
b = 0;
if(day>4) b = -10;
}
}
var jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;
b = 0;
if(jd>2299160){
a = Math.floor((jd-1867216.25)/36524.25);
b = 1+a-Math.floor(a/4.);
}
var bb = jd+b+1524,
cc = Math.floor((bb-122.1)/365.25),
dd = Math.floor(365.25*cc),
ee = Math.floor((bb-dd)/30.6001);
day =(bb-dd)-Math.floor(30.6001*ee);
month = ee-1;
if(ee>13) {
cc += 1;
month = ee-13;
}
year = cc-4716;
var wd = gmod(jd+1,7)+1,
z = jd-epochastro,
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
var j = Math.floor((z-shift1)/iyear),
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
var im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
var id = z-Math.floor(29.5001*im-29);
var result = {
gregorian_day : day,
gregorian_month : month -1,
gregorian_year : year,
julian_day : jd-1,
weekday : wd-1,
hijri_day : id,
hijri_month : im-1,
hijri_year : iy
}
return result;
}
function getIslamicDate(date) {
return writeIslamicDate(kuwaiticalendar(date));
}
function writeIslamicDate(dateResult) {
var outputIslamicDate = DAYNAMES[dateResult.weekday] + ", "
+ dateResult.hijri_day + " " + MONTHNAMES[dateResult.hijri_month] + " " + dateResult.hijri_year + " AH";
return outputIslamicDate;
}
// public API
exports.getIslamicDate = getIslamicDate;
exports.getIslamicDateParts = kuwaiticalendar;
exports.MONTHNAMES = MONTHNAMES;
exports.DAYNAMES = DAYNAMES;
})(typeof exports === 'undefined' ? this['IC'] = {} : exports);