Search code examples
c#.netdatecalendargregorian-calendar

How to convert Gregorian date to Coptic date in C#


How to convert Gregorian calendar to Coptic calendar in C#??

I have the following code:

public enum CopticMonth
{
    Thout = 1,
    Paopi = 2,
    Hathor = 3,
    Koiak = 4,
    Tobi = 5,
    Meshir = 6,
    Paremhat = 7,
    Parmouti = 8,
    Pashons = 9,
    Paoni = 10,
    Epip = 11,
    Mesori = 12,
    PiKogiEnavot = 13
}

public class CopticDate
{
    public int Day { get; set; }
    public CopticMonth Month { get; set; }
    public int Year { get; set; }
}

I need to implement the following method

public static CopticDate ToCopticDate(this DateTime date)
{
    //...        
}

Solution

  • Like Marc, I have NodaTime in my toolbox for jobs like this. You'll need to install the Nuget package.

    The implementation is simple - we create a LocalDate object from the input date, and convert it to the Coptic calendar using .WithCalendar(CalendarSystem.Coptic). We then return an instance of your class:

    public static CopticDate ToCopticDate(this DateTime date)
    {
        var localDate = LocalDate.FromDateTime(date, CalendarSystem.Gregorian)
                                 .WithCalendar(CalendarSystem.Coptic);
    
        return new CopticDate
        {
            Day = localDate.Day,
            Month = (CopticMonth)localDate.Month,
            Year = localDate.Year
        };
    }
    

    With an input date of 6 September 2019, I get the following output:

    Day: 1

    Month: PiKogiEnavot

    Year: 1735

    which so far as I can tell is the expected output.