Date Conversion yyddd to mm/dd/yyyy
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
Mike GoldPosted Apr 30, 2007, 1:45 PM
Huang ElitePosted Apr 28, 2007, 11:21 PM
Mike GoldPosted Apr 28, 2007, 11:19 PM
Glad it worked for you :-)
ChongoPosted Apr 27, 2007, 4:21 PM
Mike GoldPosted Apr 27, 2007, 1:06 PM
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();