0

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.

Community
  • 1
  • 1
Relativity
  • 6,690
  • 22
  • 78
  • 128
  • The links you posted pertain to "Julian day numbers", as used in astronomy. As you noted, they are typically expressed as floating point day number and fractional day. But it appears that what you actually have is a date in the "Julian calendar", which would be a completely different conversion. – Jim Lewis Feb 18 '16 at 20:11
  • Any idea how do I do that? – Relativity Feb 18 '16 at 21:11
  • Two questions. First, do you have the 1104-08-16 already in a c# program, and if so, what kind of class does it belong to, string? Second, whether you would like to use the DateTime or DateTimeOffset class depends on whether you know (and care about) the time zone in which the date occurred, so do you know/care? – Gerard Ashton Feb 19 '16 at 00:48
  • 1104-08-16 is string (we read it from a pipe separated text file). I think I don't have to look for time zone, so I will use datetime. – Relativity Feb 19 '16 at 18:57

1 Answers1

1

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();
        }
    }
}
Gerard Ashton
  • 560
  • 4
  • 16