By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

DateTime with only Year and Day of Year

P: n/a
Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?

Oct 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I would first get the first day of the year and subtract one, like so:

DateTime lastDayOfPreviousYear = (new DateTime(year, 1, 1)).AddDays(-1);

Then, you can add the number of days to the lastDayOfPreviousYear and it
will give you the appropriate DateTime:

DateTime dateTime = lastDayOfPreviousYear.AddDays(days);

Of course, you can just get the first date of the year, and subtract one
from the number of days, like so:

DateTime firstDayOfYear = new DateTime(year, 1, 1);
DateTime dateTime = firstDayOfYear.AddDays(days - 1);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"O.B." <fu******@bellsouth.netwrote in message
news:11********************@e9g2000prf.googlegroup s.com...
Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?

Oct 17 '07 #2

P: n/a
O.B. wrote:
Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?
No, but you could create a DateTime for your year, and add the number of
days to it like:

int year = 2007;
int day = 186;

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

MessageBox.Show(d.ToShortDateString() + "\n" + d.DayOfYear.ToString());
The subtraction is necessary or you'll end up on day 187.

Chris.
Oct 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.