Skip to content

Date Conversion yyddd to mm/dd/yyyy

Chongo

Asked Apr 27, 2007, 8:14 AM EDT5 answers
Share
LinkedInX
I have a structure that has the date stored in yyddd (Ordinal ISO 8601 format), I need to convert this to Gregorian Format or MM/dd/yyyy. I have tried and tried and have not come up with a good way to do this. does anyone have any hints? Example: 07117 = 04/27/2007 (ddd = DayOfYear) Thanks

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    I have a structure that has the date stored in yyddd (Ordinal ISO 8601 format), I need to convert this to Gregorian Format or MM/dd/yyyy. I have tried and tried and have not come up with a good way to do this. does anyone have any hints? Example: 07117 = 04/27/2007 (ddd = DayOfYear) Thanks
     
    Hi,

    This is an interesting problem because the DateTime class won't parse ISO 8601.  The trick to doing this is to use the TimeSpan class to add on the days:

                string iso8601 = "07117";
                int year = Convert.ToInt32(iso8601.Substring(0, 2)) + 2000;
                int days = Convert.ToInt32(iso8601.Substring(2, 3));
                DateTime dt = new DateTime(year, 1, 1) + new TimeSpan(days - 1, 0, 0, 0);
                Console.WriteLine("The date is = {0}", dt.ToString("MM/dd/yyyy"));
                Console.ReadLine();
    0
  • 0
    Thank you very much mike. The code worked perfectly, I wasted so much time on this too.
    0
  • Mike Gold
    0
    Hi Daniel,

    Glad it worked for you :-)
    0
  • 0
    If year is a leap year,it's caculator all rights?
    0
  • Mike Gold
    0
    I think so since it is adding the days to the current year, but it's a good question.
    0