
An easy way to calculate a Julian date in C#
A Julian date is just the number of consecutive days after midday on the 31st of January 4713BC. They are necessary for many astronomy applications. I’ve used the Julian date system for various calculations in all three of my space themed apps.
In the original C code for Solar System Explorer, I did it the “hard way”. I used a fairly well known method that is complicated because it has to take into account the oddities of the Gregorian calendar, which is designed to follow the seasons.
I had to re-implement the astronomy library I’d created for handling Julian dates when I re-wrote Solar System Explorer. It was pure chance that lead me to this post on stack exchange showing a really simple way of calculating the Julian Date in c#.
Surprisingly, C# actually has a built in function that almost does the job.
public static double calcJulianDate(System.DateTime date) { return date.ToOADate() + 2415018.5; }
The OADate in ToOADate stands for OLE Automation date. This is the consecutive number of days since midnight on the 31st December 1899.
A Julian date is also a count of the number of consecutive days. The difference is that it starts from midday on the 1st of January, 4713 BC.
Because both date systems simply count days, it’s quite easy to calculate the Julian date in C#. Just add or subtract the number of days between midday 1st January 4713BC and midnight 31st December 1988.
So, what is an OLE Automation date? It refers to a an inter-process communication technology developed by Microsoft that was intended for use with scripting languages like Visual Basic.