如何在C#中從day of year
獲得date
?如何從年初獲取日期
我有這樣的代碼:
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
我需要得到的價值22.2.2014
。但這不起作用,我該怎麼辦?提前致謝。
如何在C#中從day of year
獲得date
?如何從年初獲取日期
我有這樣的代碼:
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
我需要得到的價值22.2.2014
。但這不起作用,我該怎麼辦?提前致謝。
int a = 53;
int year = DateTime.Now.Year; //Or any year you want
DateTime theDate = new DateTime(year, 1, 1).AddDays(a - 1);
string b = theDate.ToString("d.M.yyyy"); // The date in requested format
非常感謝你,這工作完美,只需要更正toString ToString –
int a = 53;
var dt = new DateTime(DateTime.Now.Year, 1, 1).AddDays(a - 1);
string b = dt.ToString();
非常感謝!有效。 –
假設您想當年?
int a = 53;
DateTime date = new DateTime(DateTime.Now.Year, 1,1).AddDays(a -1);
謝謝你的回答。 –
使用
DateTime.AddDays()
初始化的日期開始的一年,那麼就使用此功能
http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx
謝謝你的回答。 –
你可能需要使用TimeSpan.FromDays添加
a
到( x):http://msdn.microsoft.com/en-us/library/system.timespan.fromdays(v=vs.110).aspx然後使用該值將其轉換爲DateTime。 –謝謝你的評論。 –