Convert Julian Date with Time (H/m/s) to Date Time in C#

Deadly 提交于 2019-12-04 17:13:31

Thanks to Mr. Zator answer here I was able to solve my problem like so:

public DateTime JulianToDateTime(double julianDate) {
    double unixTime = (julianDate - 2440587.5) * 86400;

    DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    dtDateTime = dtDateTime.AddSeconds(unixTime).ToLocalTime();

    return dtDateTime;
}

It is worth mentioning though, that this only works for CE Julian Date types, if the Julian Date is in BCE type it will not work, someother function is needed for that. I also made the opposite version of this method that looks like this:

public double DateTimeToJulian(DateTime dateTime) {
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = dateTime.ToUniversalTime() - origin;
    double unixTime = Math.Floor(diff.TotalSeconds);
    double julianDate = (unixTime / 86400) + 2440587.5;

    return julianDate;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!