Utilizing the known values here are two solutions I have personally utilized which I first did in NodeJS then converted to MSSQL when trying to get Julian Microsecond.
I had a known value in Gregorian and Julian Microsecond and so I just had to convert bridge the two and used https://www.aavso.org/jd-calculator as a check along the way. I used this answer to
/* Known Values:
Gregorian: 2021-03-03 21:55:00.000
Julian; 2459277.41319 )
JulianMicrosecond: 212481593700000000
*/
I used this answer along the way to develop my Nodejs code.
let getJulainDay = (Year,Month,Day) => {
a = parseInt((14 - Month) / 12);
y = Year + 4800 - a;
m = Month + 12 * a - 3;
JDN =
(Day +
parseInt(
(
(153 * m + 2) / 5) +
(365 * y) +
parseInt(y / 4) -
parseInt(y / 100) +
parseInt(y / 400) -
32045 )
)
//Into Julian Microseconds 24*60*60*1e6
* 24 * 60 * 60 * 1000000;
return JDNMicroSeconds;
};
Then I mapped this into MSSQL that I had to treat a bit differently, noting that I had to enter all integer values in the formula above as floats or I would have had to cast each result.. it was just easier to enter them as floats.. if I don't do this I only get integer results and it throws the results off by years.
This is definitely an overly verbose breakdown but it allows you to see the steps along the way.
DECLARE @date DATETIME;
SET @date = '2021-03-03 21:55:00.000';
SELECT @date,
YEAR(@date) as Year,
DATEPART(MONTH,@date) as Month,
DATEPART(DAY,@date) as Day,
DATEPART(DAYOFYEAR, @date) as DofY,
14 -(DATEPART(MONTH, @date)) as mm,
((14.0 -(DATEPART(MONTH, @date))) / 12.0) as A,
(YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) as Y,
( DATEPART(MONTH,@date) + (12.0 * ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) -3.0) as M,
/* Watch the order of operations/parethesis! */
(DATEPART(DAY,@date) +
((153.0 * (( DATEPART(MONTH,@date) + (12.0 * ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) -3.0)) + 2.0) / 5.0) +
(365.0 * ((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)))) +
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 4.0) -
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 100.0) +
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 400.0) -
32045.0) as JDN,
(DATEPART(DAY,@date) +
((153.0 * (( DATEPART(MONTH,@date) + (12.0 * ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) -3.0)) + 2.0) / 5.0) +
(365.0 * ((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)))) +
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 4.0) -
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 100.0) +
((YEAR(@date) + 4800.0 - ((14.0 -(DATEPART(MONTH, @date))) / 12.0)) / 400.0) -
32045.0) * 24 * 60 * 60 * 1000000 as JDNMicroSecond,
/* What others proposed which I do not believe is correct. */
(YEAR(@date) - 1900) * 1000 + DATEPART(DAYOFYEAR, @date) IncorrectJDN;
The output being:
