Convert Julian Date in C#

Today in this article, we shall see how to How to Convert Julian Date in C# code to the normal date and vice versa.

Today in this article, we will cover below aspects,

What is Julian date

Julian date is date format that is a combination of the current year and the number of days since the beginning of the year.

For example,

July 25, 2020, is represented as 2020206

December 31, 2020, is represented as 2020365.

Convert Julian Date in C

Example– Like identifying the total days between two doctors’ visits etc.

public static DateTime GetDateFromJulian(string julianDate)
        {
            int year = Convert.ToInt32(julianDate.Substring(2, 2));
            int day = Convert.ToInt32(julianDate.Substring(4));
            DateTime dt = new DateTime(1999 + year, 12, 18, new JulianCalendar());

            dt = dt.AddDays(day);

            return dt;

        }

Example1

2020206

Above date represent Year of 2020 and 206th day of the year.

Date Converted

7/25/2020

Example2

2018334

Date

11/30/2018

Date format example,

Let’s take an example with another format.

19168

Above date represent Year of 2019 and 168th day of the year.

Date

6/17/2019

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *