Is this possible to change Persian date to Gregorian date?
For example I have this date: 1396-12-04
, I want to convert it to 2019-02-23
.
Asked
Active
Viewed 1.3k times
13

Mohammadreza Yektamaram
- 1,392
- 16
- 36

bita
- 335
- 2
- 9
- 22
2 Answers
24
Use this:
JalaliDate = {
g_days_in_month: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
j_days_in_month: [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
};
JalaliDate.jalaliToGregorian = function(j_y, j_m, j_d) {
j_y = parseInt(j_y);
j_m = parseInt(j_m);
j_d = parseInt(j_d);
var jy = j_y - 979;
var jm = j_m - 1;
var jd = j_d - 1;
var j_day_no = 365 * jy + parseInt(jy / 33) * 8 + parseInt((jy % 33 + 3) / 4);
for (var i = 0; i < jm; ++i) j_day_no += JalaliDate.j_days_in_month[i];
j_day_no += jd;
var g_day_no = j_day_no + 79;
var gy = 1600 + 400 * parseInt(g_day_no / 146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
g_day_no = g_day_no % 146097;
var leap = true;
if (g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
{
g_day_no--;
gy += 100 * parseInt(g_day_no / 36524); /* 36524 = 365*100 + 100/4 - 100/100 */
g_day_no = g_day_no % 36524;
if (g_day_no >= 365) g_day_no++;
else leap = false;
}
gy += 4 * parseInt(g_day_no / 1461); /* 1461 = 365*4 + 4/4 */
g_day_no %= 1461;
if (g_day_no >= 366) {
leap = false;
g_day_no--;
gy += parseInt(g_day_no / 365);
g_day_no = g_day_no % 365;
}
for (var i = 0; g_day_no >= JalaliDate.g_days_in_month[i] + (i == 1 && leap); i++)
g_day_no -= JalaliDate.g_days_in_month[i] + (i == 1 && leap);
var gm = i + 1;
var gd = g_day_no + 1;
gm = gm < 10 ? "0" + gm : gm;
gd = gd < 10 ? "0" + gd : gd;
return [gy, gm, gd];
}
And example:
var myDate = "1397-12-04",
dateSplitted = myDate.split("-"),
jD = JalaliDate.jalaliToGregorian(dateSplitted[0], dateSplitted[1], dateSplitted[2]);
jResult = jD[0] + "-" + jD[1] + "-" + jD[2];
console.log(jResult);

Younes
- 462
- 7
- 15

Mohammadreza Yektamaram
- 1,392
- 16
- 36
-
Thanks @Mohammadreza but `( 1397, 12, 04 )` is going to be dynamic instead of static. – bita Feb 23 '19 at 06:35
-
Of course @bita , I've edited above code and you can set static date. – Mohammadreza Yektamaram Feb 23 '19 at 06:38
-
Thanks a lot @Mohammadreza. But there is an issue.For a specific case the result of `jD` is `1990,3,5`. I want to be `1990-03-05` – bita Feb 23 '19 at 07:33
-
And why the result of ` var jDSplitted = jD.split(',')` is `TypeError: jD.split is not a function` – bita Feb 23 '19 at 07:36
-
I try this one : ` jD.replace(/,/g, "-")` but there is an error: ` jD.replace is not a function` – bita Feb 23 '19 at 07:46
-
Dear @bita, I've customized my javascript code for you, please test it again. – Mohammadreza Yektamaram Feb 23 '19 at 08:14
-
1Great.That's exactly what I was looking for.Thank you @Mohammadreza :) – bita Feb 23 '19 at 08:24
-
Thank you @MohammadrezaYektamaram. have you wrote gregorian to jalali too? – Seyed Reza Dadrezaei Feb 24 '20 at 14:48
-
@SeyedRezaDadrezaei Yes, but I can't put it here, I've in my website, please get it from Javascript in website source: timestamp.ir – Mohammadreza Yektamaram Feb 24 '20 at 16:40
3
There is a library exactly for this : jalali-moment
The steps for it are quite simple and are well documented.
This plugin provides using jalali and gregorian calendar system together on momentjs api.
.locale('fa'); it will use jalali calendar system
.locale('any other locale'); it will use gregorian calendar system
You can also do it in this way as described in this stack overflow answer.

Community
- 1
- 1

rishichawda
- 423
- 4
- 11
-
It is not possible to do it without any library or plugin ?I don't want to use any extra plugin? – bita Feb 23 '19 at 06:15
-
1
-
-
1I had also given a link to the stackoverflow answer which solves it with jQuery. :) Please check that if it works for you. – rishichawda Feb 23 '19 at 06:37