我有一个PHP函数将公历转换为Jalali日历。我通过在线转换器将PHP函数转换为此JavaScript函数,但它在运行时向我显示了两个错误。
看看这段代码。它向我展示了两个错误。
1-未捕获的SyntaxError:意外的标识符
2-未捕获的ReferenceError:未定义gregorian_to_jalali
我找不到问题。
<script>
//Function to convert Gregorian calendar to Jalili calendar to save it in the database.
function gregorian_to_jalali(g_y, g_m, g_d, mod) {
if (typeof mod === "undefined") {
mod = '';
}
var d_4 = g_y % 4;
var g_a = {
0: 0,
1: 0,
2: 31,
3: 59,
4: 90,
5: 120,
6: 151,
7: 181,
8: 212,
9: 243,
10: 273,
11: 304,
12: 334
};
var doy_g = g_a[g_m] + g_d;
if (d_4 == 0 AND g_m > 2) {
doy_g++;
}
var d_33 = (((g_y - 16) % 132) * .0305);
var a = (d_33 == 3 OR d_33 < (d_4 - 1) OR d_4 == 0) ? 286 : 287;
var b = ((d_33 == 1 OR d_33 == 2) AND(d_33 == d_4 OR d_4 == 1)) ? 78 : ((d_33 == 3 AND d_4 == 0) ? 80 : 79);
if (((g_y - 10) / 63) == 30) {
a--;
b++;
}
if (doy_g > b) {
var jy = g_y - 621;
var doy_j = doy_g - b;
} else {
jy = g_y - 622;
doy_j = doy_g + a;
}
if (doy_j < 187) {
var jm = ((doy_j - 1) / 31);
var jd = doy_j - (31 * jm++);
} else {
jm = ((doy_j - 187) / 30);
jd = doy_j - 186 - (jm * 30);
jm += 7;
}
return (mod == '') ? {
0: jy,
1: jm,
2: jd
} : jy + "" + mod + "" + jm + "" + mod + "" + jd;
}
答案 0 :(得分:1)
您使用“AND”和“OR”代替“&amp;&amp;”和“||”分别。这是你的代码:
function gregorian_to_jalali(g_y, g_m, g_d, mod) {
if (typeof mod === "undefined") {
mod = '';
}
var d_4 = g_y % 4;
var g_a = {
0: 0,
1: 0,
2: 31,
3: 59,
4: 90,
5: 120,
6: 151,
7: 181,
8: 212,
9: 243,
10: 273,
11: 304,
12: 334
};
var doy_g = g_a[g_m] + g_d;
if (d_4 == 0 && g_m > 2) {
doy_g++;
}
var d_33 = (((g_y - 16) % 132) * .0305);
var a = (d_33 == 3 || d_33 < (d_4 - 1) || d_4 == 0) ? 286 : 287;
var b = ((d_33 == 1 || d_33 == 2) &&(d_33 == d_4 || d_4 == 1)) ? 78 : ((d_33 == 3 && d_4 == 0) ? 80 : 79);
if (((g_y - 10) / 63) == 30) {
a--;
b++;
}
if (doy_g > b) {
var jy = g_y - 621;
var doy_j = doy_g - b;
} else {
jy = g_y - 622;
doy_j = doy_g + a;
}
if (doy_j < 187) {
var jm = ((doy_j - 1) / 31);
var jd = doy_j - (31 * jm++);
} else {
jm = ((doy_j - 187) / 30);
jd = doy_j - 186 - (jm * 30);
jm += 7;
}
return (mod == '') ? {
0: jy,
1: jm,
2: jd
} : jy + "" + mod + "" + jm + "" + mod + "" + jd;
}
alert(gregorian_to_jalali(2013, 1, 31)[0]); //returns 1391