Javascript Pesian DatePicker 计算闰年问题

问题描述 投票:0回答:1

大家好,希望你们能帮我解决这个问题。我有一个日期选择器,我在项目中多次使用它,它是一个组件。但现在是波斯新年,我们有 1403(Shamsi),但在我的日期选择器中,它没有显示这是闰年。 它表明明年我的意思是1404年是闰年。但上一个闰年 1399 年是正确的。

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);}

这些是主函数中使用的一些函数,主函数是以下部分。我不知道需要更改哪一部分才能在日历中正确显示它,因为我更改了波斯函数中的一些数字,它就像:year - ((year > 0)? 474: 473 TO =>year - 473 它说 1403 是一个闰年,这是真的,但它在日历中没有正确显示。

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) };}
javascript twitter-bootstrap datepicker calendar
1个回答
0
投票

-> 在 'leap_persian' 和 'jalali_to_gregorian' 函数中尝试我的代码:-

->代码:-

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

->代码:-

function jalali_to_gregorian(d) {
    var adjustDay = 0;
    if(d[1] < 0 || d[1] >= 6){ // Adjust if month is before Farvardin or equal to Shahrivar
        adjustDay = leap_persian(d[0]) ? 30 : 29;
        d[1]++;
    }
    var gregorian = jd_to_gregorian(persian_to_jd(d[0], d[1] + 1, d[2]) - adjustDay);
    gregorian[1]--;
    return gregorian;
}
© www.soinside.com 2019 - 2024. All rights reserved.