Search code examples
c#gregorian-calendarjulian-date

Convert juilan date (not as double, but date) to gregorian date


I have a julian date : 1104-08-16, How do I convert it into gregorian date in c#?

I found following links...link link. But all of them use julian date value as float/decimal.

In my case julian is not float, it is actual date.

Any help would be appreciated.


Solution

  • If you don't know or care about time zones, you could try the following. I used this approach because I couldn't find a parse method that allowed you to specify which calendar to interpret the input.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Globalization;
    
    namespace julian2gregorian
    {
        class Program
        {
            private static JulianCalendar jcal;
            static void Main(string[] args)
            {
                jcal = new JulianCalendar();
                string jDateString = "1104-08-16";
                char[] delimiterChars = { '-' };
                string[] dateParts = jDateString.Split(delimiterChars);
                int jyear, jmonth, jday;
                bool success = int.TryParse(dateParts[0], out jyear);
                success = int.TryParse(dateParts[1], out jmonth);
                success = int.TryParse(dateParts[2], out jday);
                DateTime myDate = new DateTime(jyear, jmonth, jday, 0, 0, 0, 0, jcal);
                Console.WriteLine("Date converted to Gregorian: {0}", myDate);
                Console.ReadLine();
            }
        }
    }