Javascript into mathematical functions

These are working JavaScript functions a part of a larger program.

Could someone translate these into mathematical formulas non-Javascript.

I am not sure what these two expressions mean:

  1. ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)))
  2. ((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)
function leap_gregorian(year)
{
    return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)));
}
function gregorian_to_jd(year, month, day)
{
    return (1721425.5 - 1) +
           (365 * (year - 1)) +
           Math.floor((year - 1) / 4) +
           (-Math.floor((year - 1) / 100)) +
           Math.floor((year - 1) / 400) +
           Math.floor((((367 * month) - 362) / 12) +
           ((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)
           ) +
           day);
}

Hi there,

The code you posted is used to test for a leap year.

The percentage sign is known as the remainder, or modulo operator and returns the remainder from the division of the second number into the first.
So in this case it checks:

  1. Is the variable year divisible by four (i.e. there is no remainder)
  2. and is the variable year divisible by one hundred
  3. and is the variable year NOT divisible by 400

If these three conditions are met, the function returns true, otherwise it returns false.

Here we see the so-called ternary operator (?:) at work.
The syntax is as follows:
test ? expression1 : expression2
test is any Boolean expression
expression1 is returned if test is true
expression2 is returned if test is false

So what is happening here is that if the variable month is less than or equal to two, then a value of zero is returned.
Otherwise a second ternary operator checks if the function “leap_gregorian” when called with the parameter “year” returns true.
If so, it returns -1, if not it returns -2

This whole line of the function will just equate to a numeric value, to be added to the return value.

i.e.

  • month <= 2 then the value will be 0
  • month > 2 and leap_gregorian(year) returns true then the value will be -1
  • month > 2 and leap_gregorian(year) returns falsethen the value will be -2

I hope that all makes sense!

That JavaScript is a LOT more complicated than it needs to be - JavaScript has a Date object that provides the ability to do date manipulations without needing all those calculations.

The following does the same thing as all the code originally posted. Note that the code to get the Julian Day doesn’t use the code to determine leap years, I have just included that so you can see how much simpler it can be done in JavaScript than the complicated (and incomplete) formula the original code uses.

To determine whether or not a year is a leap year or not:

function leap_gregorian(year) {return ((new Date(year,1,29)).getDate() == 29);}

To convert a Gregorian Date into a Julian Day (including the correct fraction of a day - which the original code doesn’t do)

Date.prototype.getJulian = function() { return Math.floor((this / 86400000) - (this.getTimezoneOffset()/1440) + 2440587.5);}
function gregorian_to_jd(year, month, day) {return ((new Date(year, month-1, day)).getJulian());

That’s neat! :tup: