javascriptdatepickerpersian-calendar

Persian DatePicker doesn't calculate correct FirstDayOfYear for 1403 jalali


I am using the ha-datetimepicker in my web application. But for the year 1403, instead of the first day of the year starting from Wednesday, it mistakenly starts from Thursday.Apparently, the problem is related to the following piece of code, but I could not solve the problem. I would be grateful if someone could help me solve this problem.

Thank you all ...

HaSolarDate.prototype.getWeekDay = function (solarDate) {
    if (solarDate == null)
        solarDate = this;

    var month = solarDate.getMonth() + 1;
    var year = solarDate.getFullYear();
    var day = solarDate.getDate();

    if (Number(day) == "NaN" || Number(month) == "NaN" || Number(year) == "NaN")
        return "Wrong Date!";

    var firstDayOfYear95 = 1;

    var step = year < 1395 ? 1 : -1;
    var i = year;
    var leapCount = 0;
    while (i != 1395) {
        if (this.isLeapYear(i))
            leapCount++;
        i += step;
    }
    if (year > 1395)
        leapCount++;

    var offset = (firstDayOfYear95 + (leapCount * -step)) % 7;
    var firstDayOfYear = (((year - 1395) % 7) + offset) % 7;
    if (firstDayOfYear < 0)
        firstDayOfYear = 7 + firstDayOfYear;
    if (month <= 6) {
        var dayOfWeek = ((((month - 1) * 3) - 1) + day + firstDayOfYear) % 7;
    } else {
        var dayOfWeek = ((((month - 1) * 2) - 2) + day + firstDayOfYear) % 7;
    }

    dayOfWeek = Math.floor(dayOfWeek);
    if (dayOfWeek == 7)
        dayOfWeek = 0;
    return dayOfWeek;
}

Wrong calculation

Correct calendar


Solution

  • Changing this line

    if (year > 1395) leapCount++;
    

    to this one

    if (year > 1395 && !this.isLeapYear()) leapCount++;
    

    may fix the problem. The issue is that an extra day is being added to the start of each leap year, but each leap year has an extra day at the end of the year.