Can anyone help me to change Gregorian date to Persian in JavaScript? I want to use it in HTML and JavaScript.
Get 'jalali.js' from this link.
If your numbers is in Persian format, at first you have to change them to English format, from this link
var
persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
fixNumbers = function (str) {
if (typeof str === 'string') {
for (var i = 0; i < 10; i++) {
str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
}
}
return str;
};
and use such as the function 'getGregorian()' below to convert date. For instance I convert '۱۳۹۸/۰۱/۲۴' date:
function getGregorian () {
var date = ("۱۳۹۸/۰۱/۲۴").split('/');
y = Number( fixNumbers(date[0]) );
m = Number( fixNumbers(date[1]) );
d = Number( fixNumbers(date[2] ));
gDate = toGregorian(y, m, d)
gDate = gDate .gy + '/' + gDate .gm + '/' + gDate .gd;
}
You can use toLocaleDateString();
let today = new Date().toLocaleDateString('fa-IR');
console.log(today);
fa-IR
is for Farsi-Iran,
other's like bellow:
en-US
: For English
hi-IN
: For Hindi
...
non of answers in this page worked correct, its working in some cases but not always correct! this is working both side converting methods :
function gregorian_to_jalali(gy,gm,gd){
var g_d_m=[0,31,59,90,120,151,181,212,243,273,304,334];
var jy=(gy<=1600)?0:979;
gy-=(gy<=1600)?621:1600;
var gy2=(gm>2)?(gy+1):gy;
var days=(365*gy) +(parseInt((gy2+3)/4)) -(parseInt((gy2+99)/100))
+(parseInt((gy2+399)/400)) -80 +gd +g_d_m[gm-1];
jy+=33*(parseInt(days/12053));
days%=12053;
jy+=4*(parseInt(days/1461));
days%=1461;
jy+=parseInt((days-1)/365);
if(days > 365)days=(days-1)%365;
var jm=(days < 186)?1+parseInt(days/31):7+parseInt((days-186)/30);
var jd=1+((days < 186)?(days%31):((days-186)%30));
return [jy,jm,jd];
}
function jalali_to_gregorian(jy,jm,jd){
var gy=(jy<=979)?621:1600;
jy-=(jy<=979)?0:979;
var days=(365*jy) +((parseInt(jy/33))*8) +(parseInt(((jy%33)+3)/4))
+78 +jd +((jm<7)?(jm-1)*31:((jm-7)*30)+186);
gy+=400*(parseInt(days/146097));
days%=146097;
if(days > 36524){
gy+=100*(parseInt(--days/36524));
days%=36524;
if(days >= 365)days++;
}
gy+=4*(parseInt((days)/1461));
days%=1461;
gy+=parseInt((days-1)/365);
if(days > 365)days=(days-1)%365;
var gd=days+1;
var sal_a=[0,31,((gy%4==0 && gy%100!=0) || (gy%400==0))?29:28,31,30,31,30,31,31,30,31,30,31];
var gm
for(gm=0;gm<13;gm++){
var v=sal_a[gm];
if(gd <= v)break;
gd-=v;
}
return [gy,gm,gd];
}
code from : https://github.com/hat3ck/Persian-Calendar-Qt