Reputation: 3748
How can I get date
from day of year
in C#?
I have this code :
int a = 53; // This is the day of year value, that I got previously
string b = Convert.ToDateTime(a).ToString(); // Trying to get the date
I need to get the value 22.2.2014
. But this doesn't work, what should I do? Thanks in advance.
Upvotes: 25
Views: 24074
Reputation: 7311
int dayOfYear = 53;
int year = DateTime.Now.Year; //Or any year you want
DateTime theDate = new DateTime(year, 1, 1).AddDays(dayOfYear - 1);
string b = theDate.ToString("d.M.yyyy"); // The date in requested format
Upvotes: 42
Reputation: 2495
Use
DateTime.AddDays()
Initialize a date to start of the year , then just add a
to that using this function
http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx
Upvotes: 0
Reputation: 438
Assuming you want the current year?
int a = 53;
DateTime date = new DateTime(DateTime.Now.Year, 1,1).AddDays(a -1);
Upvotes: 6
Reputation: 144206
int a = 53;
var dt = new DateTime(DateTime.Now.Year, 1, 1).AddDays(a - 1);
string b = dt.ToString();
Upvotes: 1