Geeks With Blogs

Last time we had a method for converting a calendar date to Julian, so here's the reverse. Notice that it calls the previous MinSec.DecimalToDegMinSec() method to convert the decimal portion to hours/minutes/seconds:

```        public static DateTime ConvertDay(double DJ)         {             double D = DJ + 0.5;             int I = (int)Math.Floor(D);             double FD = D - I;             if (FD == 1)             {                 FD = 0;                 I += 1;             }             if (I > -115860)             {                 int A = (int)Math.Floor((I / 36524.25) + .99835726) + 14;                 I = I + 1 + A - (int)Math.Floor(Convert.ToDouble(A) / 4);             }             int B = (int)Math.Floor((I / 365.25) + .802601);             int C = I - (int)Math.Floor((365.25 * B) + .75) + 416;             int G = (int)Math.Floor(C / 30.6001);             int MN = G - 1;             double DY = C - Math.Floor(30.6001 * G) + FD;             int YR = B + 1899;             if (G > 13.5)                 MN = G - 13;             if (MN < 2.5)                 YR = B + 1900;             if (YR < 1)                 YR = YR - 1;             int ID = (int)Math.Floor(DY);```

`            MinSec.dms myDMS = MinSec.DecimalToDegMinSec(Convert.ToDecimal(FD) * 24);`

`            DateTime dt = new DateTime(YR, MN, ID, Convert.ToInt32(myDMS.degrees), Convert.ToInt32(myDMS.minutes), Convert.ToInt32(myDMS.seconds));`

```            return dt;         }```

And here's a test app:

```        public Program()         {             Console.WriteLine("Converting 32539.5: " + CalDay.ConvertDay(32539.5));             Console.WriteLine("");```

```            Console.WriteLine("Converting 2447559.5 (full date): " + CalDay.ConvertDay(2447559.5 - 2415020));             Console.WriteLine("");```

```            Console.WriteLine("Converting .8: " + CalDay.ConvertDay(.8));             Console.WriteLine("");```

```            Console.ReadLine();         }```

By default, this method is going to return the number of days elapsed since 1900 January 0.5, what the author considers the current epoch. To convert the full Julian date, just subtract 2145020 from it.

Technorati Tags: