You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

波斯历日期选择器1403年闰年识别错误问题求助

波斯历(Shamsi)日期选择器闰年计算异常问题排查

我在项目里复用一款波斯历日期选择器组件,碰到了闰年计算错误的问题:1403年本该是闰年,但组件却判定1404年为闰年,不过1399年的闰年识别是正常的。

我试过修改leap_persian函数里的年份偏移逻辑,把year - ((year > 0)? 474: 473)改成year - 473,这时候1403年的闰年判定是对了,但日历显示还是不正常,求帮忙排查修正。


核心工具函数代码

function leap_persian(year)
{ return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682; }

var PERSIAN_EPOCH = 1948320.5;

function persian_to_jd(year, month, day)
{
var epbase, epyear;

epbase = year - ((year >= 0) ? 474 : 473);

epyear = 474 + mod(epbase, 2820);

return day +
        ((month <= 7) ?
            ((month - 1) * 31) :
            (((month - 1) * 30) + 6)
        ) +
        Math.floor(((epyear * 682) - 110) / 2816) +
        (epyear - 1) * 365 +
        Math.floor(epbase / 2820) * 1029983 +
        (PERSIAN_EPOCH - 1); }


function jd_to_persian(jd)
 {var year, month, day, depoch, cycle, cyear, ycycle,
    aux1, aux2, yday;
  jd = Math.floor(jd) + 0.5;

depoch = jd - persian_to_jd(475, 1, 1);
cycle = Math.floor(depoch / 1029983);
cyear = mod(depoch, 1029983);
if (cyear == 1029982) {
    ycycle = 2820;
} else {
    aux1 = Math.floor(cyear / 366);
    aux2 = mod(cyear, 366);
    ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) +
                aux1 + 1;
}
year = ycycle + (2820 * cycle) + 474;
if (year <= 0) {
    year--;
}
yday = (jd - persian_to_jd(year, 1, 1)) + 1;
month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
day = (jd - persian_to_jd(year, month, 1)) + 1;
return new Array(year, month, day);}

主函数代码

function JalaliDate(p0, p1, p2) {
var gregorianDate;
var jalaliDate;

if (!isNaN(parseInt(p0)) && !isNaN(parseInt(p1)) && !isNaN(parseInt(p2))) {
    var g = jalali_to_gregorian([parseInt(p0, 10), parseInt(p1, 10), parseInt(p2, 10)]);
    setFullDate(new Date(g[0], g[1], g[2]));
} else {
    setFullDate(p0);
}

function jalali_to_gregorian(d) {
    var adjustDay = 0;
    if(d[1]<0){
        adjustDay = leap_persian(d[0]-1)? 30: 29;
        d[1]++;
    }
    var gregorian = jd_to_gregorian(persian_to_jd(d[0], d[1] + 1, d[2])-adjustDay);
    gregorian[1]--;
    return gregorian;
}

function gregorian_to_jalali(d) {
    var jalali = jd_to_persian(gregorian_to_jd(d[0], d[1] + 1, d[2]));
    jalali[1]--;
    return jalali;
}

function setFullDate(date) {
    if (date && date.getGregorianDate) date = date.getGregorianDate();
    gregorianDate = new Date(date);
    gregorianDate.setHours(gregorianDate.getHours() > 12 ? gregorianDate.getHours() + 2 : 0)
    if (!gregorianDate || gregorianDate == 'Invalid Date' || isNaN(gregorianDate || !gregorianDate.getDate())) {
        gregorianDate = new Date();
    }
    jalaliDate = gregorian_to_jalali([
        gregorianDate.getFullYear(),
        gregorianDate.getMonth(),
        gregorianDate.getDate()]);
    return this;
}

this.getGregorianDate = function() { return gregorianDate; }

this.setFullDate = setFullDate;

this.setMonth = function(e) {
    jalaliDate[1] = e;
    var g = jalali_to_gregorian(jalaliDate);
    gregorianDate = new Date(g[0], g[1], g[2]);
    jalaliDate = gregorian_to_jalali([g[0], g[1], g[2]]);
}

this.setDate = function(e) {
    jalaliDate[2] = e;
    var g = jalali_to_gregorian(jalaliDate);
    gregorianDate = new Date(g[0], g[1], g[2]);
    jalaliDate = gregorian_to_jalali([g[0], g[1], g[2]]);
};

this.getFullYear = function() { return jalaliDate[0]; };
this.getMonth = function() { return jalaliDate[1]; };
this.getDate = function() { return jalaliDate[2]; };
this.toString = function() { return jalaliDate.join(',').toString(); };
this.getDay = function() { return gregorianDate.getDay(); };
this.getHours = function() { return gregorianDate.getHours(); };
this.getMinutes = function() { return gregorianDate.getMinutes(); };
this.getSeconds = function() { return gregorianDate.getSeconds(); };
this.getTime = function() { return gregorianDate.getTime(); };
this.getTimeZoneOffset = function() { return gregorianDate.getTimeZoneOffset(); };
this.getYear = function() { return jalaliDate[0] % 100; };

this.setHours = function(e) { gregorianDate.setHours(e) };
this.setMinutes = function(e) { gregorianDate.setMinutes(e) };
this.setSeconds = function(e) { gregorianDate.setSeconds(e) };
this.setMilliseconds = function(e) { gregorianDate.setMilliseconds(e) };}

内容的提问来源于stack exchange,提问作者saVsa 94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:07:33