How to calculate leap year in jalali calendar?

Viewed 431

As you know, there is a leap every four years in the Jalali calendar (There are, of course, a few exceptions)

And instead of having 365 days a year, it has 366 days :)

And the other problem is that some months are 29 days and some months are 30 days and some months 31 :|

I use moment library for generating these dates ( of course using fa method)

This website which called تقویم is doing it very well.

var moment = require('moment-jalaali')
moment().format('jYYYY/jM/jD')

Now my question is how to identify which months are 29 and 30 and 31 and which year is leap?

2 Answers

In my previous project I had the same problem, You must use this method to do this

This function isn't in javascript But this algorithm work very good for your leap year

internal static bool IsLeapYear(int y)
{
    int[] matches = { 1, 5, 9, 13, 17, 22, 26, 30 };
    int modulus = y - ((y / 33) * 33);
    bool K = false;
    for (int n = 0; n != 8; n++) if (matches[n] == modulus) K = true;
    return K;
}

Here is the JavaScript version of Sina Rahmani's answer (the y / 30 should round down for it to work properly in JavaScript):

function isLeapYearJalali(year) {
    const matches = [1, 5, 9, 13, 17, 22, 26, 30];
    const modulus = year - (Math.floor(year / 33) * 33);
    let isLeapYear = false;

    for (let i = 0; i != 8; i++) {
        if (matches[i] == modulus) {
            isLeapYear = true;
        }
    }

    return isLeapYear;
}
Related