-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeStampToJalaliDate.js
50 lines (37 loc) · 1.89 KB
/
timeStampToJalaliDate.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
timeStampToJalaliDate(timeStamp) {
let year = timeStamp.getFullYear(),
month = timeStamp.getMonth(),
day = timeStamp.getDay(),
jalaliDate = gregoryToJalaliDate(year, month, day);
function gregoryToJalaliDate(year, month, day) {
let gregoryMonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
jalaliMonthDays = new Array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29),
gregoryYear = year - 1600,
gregoryMonth = month - 1,
gregoryDay = day - 1,
gregoryDayNumber = 365 * gregoryYear + div(gregoryYear + 3, 4) - div(gregoryYear + 99, 100) + div(gregoryYear + 399, 400);
for (var i = 0; i < gregoryMonth; ++i)
gregoryDayNumber += gregoryMonthDays[i];
if (gregoryMonth > 1 && ((gregoryYear % 4 == 0 && gregoryYear % 100 != 0) || (gregoryYear % 400 == 0)))
gregoryDayNumber++;
gregoryDayNumber += gregoryDay;
let jalaliDayNumber = gregoryDayNumber - 79,
j_np = div(jalaliDayNumber, 12053);
jalaliDayNumber = jalaliDayNumber % 12053;
let jalaliYear = 979 + 33 * j_np + 4 * div(jalaliDayNumber, 1461);
jalaliDayNumber %= 1461;
if (jalaliDayNumber >= 366) {
jalaliYear += div(jalaliDayNumber - 1, 365);
jalaliDayNumber = (jalaliDayNumber - 1) % 365;
}
for (var i = 0; i < 11 && jalaliDayNumber >= jalaliMonthDays[i]; ++i)
jalaliDayNumber -= jalaliMonthDays[i];
let jalaliMonth = i + 1,
jalaliDay = jalaliDayNumber + 1;
function div(x, y) {
return Math.floor(x / y);
}
return jalaliYear + '/' + jalaliMonth + '/' + jalaliDay;
};
return jalaliDate;
}