【问题标题】:How to convert Gregorian date to Coptic date in C#如何在 C# 中将公历日期转换为科普特日期
【发布时间】:2019-07-14 20:22:45
【问题描述】:

如何在C#中将公历转换为科普特历??

我有以下代码:

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; }
}

我需要实现以下方法

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

【问题讨论】:

标签: c# .net date calendar gregorian-calendar


【解决方案1】:

和 Marc 一样,我的工具箱中有 NodaTime 来处理此类工作。您需要安装Nuget package

实现很简单——我们根据输入日期创建一个LocalDate 对象,并使用.WithCalendar(CalendarSystem.Coptic) 将其转换为科普特日历。然后我们返回你的类的一个实例:

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
    };
}

输入日期为 2019 年 9 月 6 日,我得到以下输出:

日期: 1

月份: PiKogiEnavot

年份: 第1735章

so far as I can tell 是预期的输出。

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多