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
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted Apr 27, 2007, 9:06 AM EDT
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();
Chongo85 pointsPosted Apr 27, 2007, 12:21 PM EDT
Mike GoldRank #50 · 30.6K pointsPosted Apr 28, 2007, 7:19 PM EDT
Glad it worked for you :-)
Huang Elite6 pointsPosted Apr 28, 2007, 7:21 PM EDT
Mike GoldRank #50 · 30.6K pointsPosted Apr 30, 2007, 9:45 AM EDT