2

I convert from UTC to Julian Date. But I need to convert Julian Date to UTC. I researched but I didn't find any code in c#

I did UTC to Julian Date I need Julian Date to UTC

iLLkeeN Nemo
  • 41
  • 10

2 Answers2

2

Create this extension method:

public static class DoubleExtensions
{
    public static DateTime JulianDateToUtc(this double julianDate)
    {
        var sinceEpoch = julianDate - 2440587.500000D;

        return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddDays(sinceEpoch);
    }
}

Then you can just do:

2458324.500000D.JulianDateToUtc();
Stevo
  • 1,424
  • 11
  • 20
  • thanks! P.S **Edit this to** `JulianDateToUtc(2458324.500000D);` – iLLkeeN Nemo Jul 25 '18 at 12:31
  • No problem. If you want to use it that way, get rid of the "this" keyword in the message signature. See: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – Stevo Jul 25 '18 at 12:33
  • I am sorry for disturbing. I have a problem. So I convert UTC (any date) to Julian Date, when I try converting **Julian Date to UTC** I always get the same date `(25.07.2018)`. How can I solve? – iLLkeeN Nemo Aug 10 '18 at 18:31
0

You can use the NodaTime library FromJulianDayNumber method to do the conversion to an instant from which you can get UTC, it will handle all of the minutia that the accepted answer does not handle.

MarkovskI
  • 1,489
  • 2
  • 21
  • 25