C# Convert between Gregorian and JDE Dates

JDE supports a julian type date format counting days from the year 1900 in the format CYYDDD. Below are helpers that may be used when mapping to and from JDE.

Convert Gregorian to JDE Julian

        public string Convert_GregorianDate_To_JulianDate(string gregorianDate)
        {
            string jdeDate = string.Empty;

            DateTime parsedDate;

            if (DateTime.TryParseExact(gregorianDate, "yyyyMMddHHmm", null, System.Globalization.DateTimeStyles.None, out parsedDate))
            {
                jdeDate = (1000 * (parsedDate.Year - 1900) + parsedDate.DayOfYear).ToString();
            }

            return jdeDate;
        }

Convert from JDE Julian to Gregorian

        public static string Convert_JDEDate_To_GregorianDate(string jdeDate)
        {
            string day = string.Empty;
            string year = string.Empty;

            if (jdeDate.Length == 5)
            {
                // 5 character string for pre-year 2000 dates, year is first 2 chars
                year = jdeDate.Substring(0, 2);
            }
            else if (jdeDate.Length == 6)
            {
                // 6 characters for post 2000 dates, take first three characters
                year = jdeDate.Substring(0, 3);
            }
            else
            {
                return string.Empty;
            }

            // take last three characters for day
            day = jdeDate.Substring(jdeDate.Length - 3, 3);

            // calculate gregorian date
            DateTime gregorianDate = DateTime.Parse("1 Jan 1900");
            gregorianDate = gregorianDate.AddYears(Convert.ToInt16(year));
            gregorianDate = gregorianDate.AddDays(Convert.ToInt16(day) - 1);

            return gregorianDate.ToString("ddMMyyyy");
        }
Advertisements
This entry was posted in .NET, BizTalk Server. Bookmark the permalink.

2 Responses to C# Convert between Gregorian and JDE Dates

  1. Tanzer says:

    That saved my life. Many many thanks.

  2. JiriB says:

    Thanks a lot! That helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s