Correct the Gregorian date being calculated is within the month before a leap year

If the Gregorian date being calculated is within the month before a leap year (for example Dec 1999 or Dec 2011) then a wrong value is returned. 

The issue is with the new Date constructor call and the calculation of the Gregorian leap year. Because the rest of the algorithm uses a 1-based month, the variable month is set to 12. But the Date constructor is 0-based and when new Date(1999, 12, 1) is called it returns Jan 1 2000 which turns out to be a leap year. This makes Gregorian date returned to be 1 less then actual it should be.