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
.
Add a comment
|
Use it:
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 );
-
Thanks @Mohammadreza but
( 1397, 12, 04 )
is going to be dynamic instead of static. – bita Feb 23 '19 at 6:35 -
Of course @bita , I've edited above code and you can set static date. – Mohammadreza Yektamaram Feb 23 '19 at 6:38
-
Thanks a lot @Mohammadreza. But there is an issue.For a specific case the result of
jD
is1990,3,5
. I want to be1990-03-05
– bita Feb 23 '19 at 7:33 -
And why the result of ` var jDSplitted = jD.split(',')` is
TypeError: jD.split is not a function
– bita Feb 23 '19 at 7:36 -
1
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.
-
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 6: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 6:37