Search code examples
c#datetimeordinal

Ordinal Date Format C#


Problem:

You are given dates in a format of YYYYddd, which is the year, followed by the day of the year, 1 through 365(366). For example today would be 2009135 (5/15/2009).

What is the best way to create a new datetime from this? In Ruby I know there is a constructor that takes two numbers like so

Date.ordinal(2009, 135)

Is there a similar Constructor for C#?


Solution

  • Hmm, I am not sure if there is a more direct way, but this should work

    new DateTime(year, 1, 1).AddDays(day - 1);